Say I have a UDP server:
import socket
ls = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ls.bind(('0.0.0.0', 12001))
while True:
print('{!r}'.format(ls.recvfrom(100)))
If I attempt to send to this server with:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.sendto(b'Test.', ('192.168.1.154', 12001))
I get a successful send, but nothing is ever emitted from the client. It doesn't matter if the packet is send locally, or from another machine on the LAN, or from a machine on the Internet (with port-forwarding set up on the router): nothing is ever received.
If I send it on localhost
, I do get the packet:
client.sendto(b'Test.', ('127.0.0.1', 12001))
That packet is emitted on the server.
Changing the port doesn't change anything / I am picking 12001 specifically to be unlikely to collide. (But this is also the third such port I've chosen.)
This smells like macOS has a firewall somewhere, but where?
(a. note that we are binding to all interfaces on the UDP server. b. even if I bind to the specific interface, nothing, although the sendto
to localhost no longer functions in that case, as one would expect. c. I can see the UDP packets arriving in Wireshark, so it is not a connectivity issue)
pfctl -d
It’s not enabled by default, but it could have been enabled at some point.pfctl -d
, still no change in behavior, though. It was, however, enabled.