Python select.select () on Windows

I am testing UDP stamping using the code here . It works on Linux, but reports an error on Windows. Here is the code snippet where the error occurs:

while True:
    rfds, _, _ = select([0, sockfd], [], [])  # sockfd is a socket
    if 0 in rfds:
        data = sys.stdin.readline()
        if not data:
            break
        sockfd.sendto(data, target)
    elif sockfd in rfds:
        data, addr = sockfd.recvfrom(1024)
        sys.stdout.write(data)

And the error message:

Traceback (most recent call last):
  File "udp_punch_client.py", line 64, in <module>
    main()
  File "udp_punch_client.py", line 50, in main
    rfds, _, _ = select([0, sockfd], [], [])
select.error: (10038, '')

I know this error has something to do with the implementation selecton Windows, and all this is quoted:

Note File objects on Windows are not acceptable, but sockets. On Windows, the main select () function is provided by the WinSock library and does not process file descriptors that do not arise from WinSock.

I had two questions:

  • What does mean 0in [0, sockfd]? Is this some commonly used method?
  • If it selectworks only with socketWindows, how to make the code compatible with Windows?

Thank.

+4
2

, select stdin , select Windows. stdin . :

  • stdin. . Python , -.
  • A greenlet- , gevent, - , . twisted (. ), -. , , (twisted gevent, ). , , twisted async stdin Windows ( , * nix, , , select).
  • . .
+4

, , . :

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()
+2

Source: https://habr.com/ru/post/1530611/


All Articles