pythonsocketstcpudptunneling

How to send data with Python over UDP from remote machine (client) to local machine (server)?


I am trying to send data with Python over UDP from a remote machine in the cloud to my local machine listening.

Run from my local machine ... (server)

from socket import *

serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind(('0.0.0.0', 3000))

while True:
    message, address = serverSocket.recvfrom(1024)
    print(message, address)

Run from the remote machine in the cloud ... (client)

from socket import *

clientSocket = socket(AF_INET, SOCK_DGRAM)

server_addr = (<WHAT GOES HERE?>, 3000)

clientSocket.sendto(b'hello world', server_addr)

I believe <WHAT GOES HERE?> should be the hostname of my local machine, but I am unfamiliar with how to expose the port on my local machine which the server is running on.

How do I expose my local machine such that the client running on the remote cloud machine can successfully send the UDP packet to the server on the local machine?

Thanks in advance.


Solution

  • Since the remote client is "in the cloud" (ie, is running on another machine elsewhere on the Internet), it will need to use the public IP that is assigned to you by your ISP. On the server side, you can use websites like https://api.ipify.org to discover your public IP and then give it to your client to connect to. Or, you can register a static hostname with any public domain Registrar (GoDaddy, etc) and point it to your public IP (if your IP is dynamic, there are Dynamic DNS services available to handle that scenario).

    If your server machine is connected DIRECTLY to your Internet modem, then your public hostname/IP will route directly to your server machine.

                     -------
         ----------> | DNS | ----------
         |           -------          |
         |                           \|/
    ----------     ------------     --------------
    | client | <-> | Internet | <-> |x:x| server |
    ----------     ------------     --------------
    

    But, if your server is NOT DIRECTLY connected to your modem, but is running behind a NAT router instead, then your public hostname/IP will route to the router, not to your server machine:

                     -------
         ----------> | DNS | ----------
         |           -------          |
         |                           \|/
    ----------     ------------     --------------     --------------
    | client | <-> | Internet | <-> |x:x| router | <-> |x:x| server |
    ----------     ------------     --------------     --------------
    

    So, the router will have to be configured separately with port forwarding rules to route inbound traffic from its public WAN IP/port to your server machine's private LAN IP/port.

    If the router supports uPNP and that is enabled, your server can configure those rules dynamically in code when binding/closing its listening socket. Otherwise, you will have to configure the rules manually using your router's admin interface.