[Python] Windows host can't receive UDP packets from WSL-2 guest

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to have a simple UDP echo client/server communicate with each other. The client program (which runs in the host Windows) sends packets to the server (which runs in WSL-2), and the server receives them, but the server's reply is never reaches the client.

    import sys
    from socket import *
    from select import select

    client = sys.argv[1].startswith("c")
    host = sys.argv[2] if len(sys.argv) > 2 else "127.0.0.1"
    port = 8080
    sd = socket(AF_INET, SOCK_DGRAM)

    def poll():
    readable, writable, errorset = select([sd], [sd], [sd], 0)
    return sd in readable

    if client:
    sd.connect((host, port))
    sd.setblocking(0)
    sd.send(b"Hello!")
    while not poll():
    pass
    data, addr = sd.recvfrom(65535)
    print(f"RECV {addr} => {data}")

    else:
    sd.bind((host, port))
    print(f"Listening on {host}:{port}")
    sd.setblocking(0)
    sd.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)

    while True:
    while poll():
    data, addr = sd.recvfrom(65535)
    print(f"RECV {addr} => {data}")
    sd.sendto(data.decode("utf-8").upper().encode("utf-8"), addr)


    The output on Windows:

    udpecho.py client 172.25.154.133


    The output on Linux:

    $ python3 udpecho.py server 172.25.154.133
    Listening on 172.25.154.133:8080
    RECV ('172.25.144.1', 57661) => b'Hello!'


    And now I'm stumped. TCP connections work ok so it must only be a UDP thing, but I don't know what to try next.

    Running Windows 10 Home edition and hosting Ubuntu-20.04 in WSL 2.

    Continue reading...

Compartilhe esta Página