python select.select() on Windows

后端 未结 2 1293
野的像风
野的像风 2021-02-09 22:05

I\'m testing UDP punching using code from here. It works on Linux however reports error on Windows. Here\'s the code snippet where the error occurs:

while True:
         


        
2条回答
  •  耶瑟儿~
    2021-02-09 22:13

    As the answer suggests, I create another thread to handle input stream and it works. Here's the modified code:

    sock_send = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    def send_msg(sock):
        while True:
            data = sys.stdin.readline()
            sock.sendto(data, target)
    
    def recv_msg(sock):
        while True:
            data, addr = sock.recvfrom(1024)
            sys.stdout.write(data)
    
    Thread(target=send_msg, args=(sock_send,)).start()  
    Thread(target=recv_msg, args=(sockfd,)).start()
    

提交回复
热议问题