How do I connect to a UDP port in Python?
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?
(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:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto('hello', 0, ('127.0.0.1', 7131))
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.
You need to call sock.connect(('127.0.0.1', 7131)) instead of bind.
精彩评论