如何在 Python 中连接到 UDP 端口?
和其他人一样,我可以说“我已经尝试了一切!”我有点这么做了。我查遍了 StackOverflow,并尝试了所有答案,但一无所获。不管怎样,在我进一步开发这个之前,我至少要先得到一些由 Python 打印的代码。
我想从我的 Garry's Mod 服务器 (logaddress_add MyIP:7131) 接收 UDP 数据包,但我似乎没有收到任何这些数据包。这很可能不是路由器防火墙问题,因为我可以在另一台计算机上使用 HLSW。我使用了 Wireshark,但没有看到来自我的服务器 IP 的任何数据。我使用了Python解释器/编写了一些代码(尽管示例是TCP )看看我是否得到了任何数据——以确保 Wireshark 没有做错任何事情——但也没有得到任何结果。我是不是在做一些傻事?
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 7131))
sock.settimeout(10)
sock.recv(1024)
编辑:我正在使用 HLSW 进行一些测试,发现它似乎有某种魔力。当您尝试 logaddress_add 某个不是 HLSW 的端口(例如 7135)时,它不会执行任何操作。 Wireshark 根本不会做任何事情。不显示任何日志,任何东西。但是,当您将 HLSW 更改为使用刚刚添加的端口 (7135) 时,Wireshark 突然获得数据流,包括我要喷射的控制台数据。 HLSW 是否正在更改某种配置?
Like everyone else, I can say "I've tried everything!" I kind of did. I looked all over StackOverflow, and tried all the answers, but got nothing. Anyways, I am jetting to at least get some code printed by Python before I get even further in developing this.
I want to receive UDP packets from my Garry's Mod server (logaddress_add MyIP:7131), and I don't seem to be receiving any of those packets. It's most likely not a router firewall problem, as I can use HLSW on my other computer. I have used Wireshark, and didn't see any data from my server's IP. I used the Python interpreter / made some code (although example was TCP) to see if I got any data--to make sure Wireshark wasn't doing anything wrong--and nothing came to it either. Am I doing something silly?
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 7131))
sock.settimeout(10)
sock.recv(1024)
Edit : I was doing some testing with HLSW, and found out it seems to be doing some kind of magic. When you try to logaddress_add the certain Port that is not HLSW (say 7135), it won't do anything. Wireshark won't do anything at all. Doesn't show any logs, anything. But, when you change HLSW to use the port that you just added (7135), Wireshark suddenly gets a flow of data, including the console data that I am jetting for. Is it some kind of configuration HLSW is changing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(不完全是一个答案,而是一个可能导致答案的诊断路径。有时,仅仅知道它实际上对其他人有效就很有帮助。)
我已将上述内容输入到 Python 控制台中,然后将以下代码输入到另一个Python控制台:
在原来的控制台上弹出消息。我在另一台机器上重复了这个实验,使用“192.168...”地址代替,它再次弹出。
(Not quite an answer, but a diagnostic path that might lead to an answer. Sometimes it helps just to know that it actually worked for someone else.)
I've entered the above into a Python console, and then typed the code below into another Python console:
The message popped out on the original console. I repeated the experiment from another machine, using a '192.168...' address instead, and it popped out again.
您需要调用 sock.connect(('127.0.0.1', 7131)) 而不是绑定。
You need to call sock.connect(('127.0.0.1', 7131)) instead of bind.