39

I have a small one sided message sender that works while I specify the IP to connect to in code, however, I am having trouble allowing the socket to accept connections from any IP. Here is the line that is the problem.

mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2727 ) )

The '' is for localhost, and it works if I manually enter IP, eg '192.168.1.106', however, how can I leave it open to all? Or am I using the wrong connection type for this?

johnthagen
9,3798 gold badges44 silver badges49 bronze badges
asked Nov 7, 2011 at 6:58

3 Answers 3

50

If you want to bind to all available IPv4 addresses, specify 0.0.0.0 as your IP address. If you're behind a router and wish to have your socket internet-accessible, rather than just available on your LAN, you'll need to set up a port forwarding rule so that users outside your LAN can access the service.

See the following ServerFault question for more info on 0.0.0.0: https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1

answered Nov 7, 2011 at 7:03
Sign up to request clarification or add additional context in comments.

4 Comments

Is it possible to bind to completely custom ips like 152.64.87.9?
@CMCDragonkai Only if your system has an interface with that address.
So I would need to create these interfaces prior to binding to them. And creating virtual interfaces would be loopbacks right?
Would that be IPv4 only? What about binding on all available IPv6 addresses?
16

Binding to '' has the same effect as to '0.0.0.0' makes the transition to IPv6 easier.

Depending on the OS, opening a socket.AF_INET6 socket listens to IPv4 and IPv6.

answered Nov 7, 2011 at 8:21

2 Comments

Python bug report to make the docs more explicit about this: bugs.python.org/issue33921
According to the docs, '' is not compatible with IPv6: This behavior is not compatible with IPv6, therefore, you may want to avoid these if you intend to support IPv6 with your Python programs.
6

Binding to 0.0.0.0 will allow it to accept connections from any IPv4 address that can route to it.

answered Nov 7, 2011 at 7:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.