Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
代码未在异步Python Websocket中打印_Python_Sockets_Asynchronous_Websocket - Fatal编程技术网

代码未在异步Python Websocket中打印

代码未在异步Python Websocket中打印,python,sockets,asynchronous,websocket,Python,Sockets,Asynchronous,Websocket,我一直在尝试使用python构建以下websocket: import socket import websockets import asyncio import traceback # Find host name HELLO_MY_NAME_IS = socket.gethostname() print(HELLO_MY_NAME_IS) # Find IP with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:

我一直在尝试使用python构建以下websocket:

import socket
import websockets
import asyncio
import traceback

# Find host name
HELLO_MY_NAME_IS = socket.gethostname()
print(HELLO_MY_NAME_IS)

# Find IP
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.connect(('8.8.8.8', 53))
    MY_IP = s.getsockname()[0]
    
print(MY_IP)

  async def port_scan():
    if not MY_IP[:3] == '192' and not MY_IP[:3] == '10.' and not MY_IP[:3] == '172':
        print('This is not a private network! SHUTTING DOWN!')
        exit()
    
    ip_range = MY_IP.split('.')
    ip_range.pop()
    ip_range = '.'.join(ip_range)
    print(ip_range)
      

async def register_client(websocket, _):
    async for message in websocket:
        print(message)

if __name__ == '__main__':
    start_server = websockets.serve(register_client, MY_IP, 1111)
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()


一切似乎都很好,没有出现错误。但是,当我运行程序时,打印(ip_范围)不会显示,它位于“async def port_scan()”中。我认为这部分的代码有问题。有人知道问题出在哪里以及如何解决吗?

我认为您的代码片段缺少一些关键的行,因为我看不到您在其中任何地方调用(使用)
port\u scan
。对-这可能是问题所在。真不敢相信我错过了这个!谢谢你的帮助,我现在就试试,看看是否对我有用。