[英]How to send and receive packets to remote server using UDP in Python?
I am trying to send and receive UDP packets in python 3 using the simple code below. 我正在尝试使用下面的简单代码在python 3中发送和接收UDP数据包。 However, this does not seem to work.
但是,这似乎不起作用。 Can someone please tell me what I am doing wrong?
有人可以告诉我我做错了吗? My program works internally on my machine, but will not receive packets from a remote server.
我的程序在计算机上内部运行,但不会接收来自远程服务器的数据包。 It should work with this server and port number : 35.185.116.250:8000.
它应该与此服务器和端口号一起使用:35.185.116.250:8000。
Another test server is 35.185.116.250:8001 另一个测试服务器是35.185.116.250:8001
Moreover, I feel like the line that may be causing problems is: clientSock.bind((socket.gethostbyname(socket.gethostname()), listening_port_number)) 而且,我觉得可能引起问题的那一行是:clientSock.bind((socket.gethostbyname(socket.gethostname()),Listening_port_number))
I am working on Windows. 我在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()
好吧,它与网络没有任何关系,所以也许您要检查一下,只是为了确保
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.