简体   繁体   中英

How to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py)

I want to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py). I don't actually know if I'm doing it right, or maybe what I did is not a good practice. How can I do that? I'm still a beginner in socket programming.

client.py(local computer)

import socket

ip = "<insert ip here>"
port = 9999
Message = "Hello, Server"

clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message.encode('utf-8'), (ip, port))

server.py(pythonanywhere)

import socket

ip = "<insert ip here>"
port = 9999

serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

serverSock.bind((ip, port))

while True:
    data, addr = serverSock.recvfrom(4096)
    print("Message: ", data)

You cannot run a udp socket server on PythonAnywhere. PythonAnywhere does not route arbitrary network traffic to the machines where you would be running the server code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM