我正在尝试使用下面的简单代码在python 3中发送和接收UDP数据包。但是,这似乎不起作用。有人可以告诉我我做错了什么吗?我的程序在我的机器上内部工作,但不会从远程服务器接收数据包。它应该与此服务器和端口号一起使用:35.185.116.250:8000。
另一个测试服务器是35.185.116.250:8001
此外,我觉得可能导致问题的行是:clientSock.bind((socket.gethostbyname(socket.gethostname()),listening_port_number))
我正在使用Windows。
import socket
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#the next line (which is commented out)works when I run my program and contact a server on my own machine
#clientSock.bind(("",listening_port_number))
my_host_IP = socket.gethostbyname(socket.gethostname())
#the following does not work for receiving packets from a remote server
clientSock.bind((my_host_IP, 8001))
def send_packets():
address = '35.185.116.250'
port = 8000
clientSock.sendto(my_host_IP.encode('utf-8'),(address,port))
print("Sending packet to address and port number: %s %s"%(address,port))
modifiedMessage, serverAddress = clientSock.recvfrom(2048)
print(modifiedMessage.decode())
send_packets()
答案 0 :(得分:-1)
嗯,它与网络没有任何关系,所以也许你检查一下,只是为了确定