简体   繁体   English

如何从我的本地计算机(client.py)发送一个简单的 UDP 消息到远程服务器 pythonanywhere(server.py)

[英]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).我想从我的本地计算机(client.py)向远程服务器 pythonanywhere(server.py)发送一条简单的 UDP 消息。 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)客户端.py(本地计算机)

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) 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 上运行 udp 套接字服务器。 PythonAnywhere does not route arbitrary network traffic to the machines where you would be running the server code. PythonAnywhere 不会将任意网络流量路由到您将运行服务器代码的机器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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