Binding to specific IP addresses

Donn Cave donn at
Thu Aug 23 19:15:50 CEST 2001

Quoth "Graham Ashton" <graz at>:
| The lsof program tells me that the process that is using the address is
| my dial up software, pppd. I find this very surprising as the
| address is the address of my ethernet card and has nothing to do with my
| dial up connection.
| Can anybody shed some light on this for me? Is there something that I
| ought to be doing to avoid listening on other interfaces?

Your Python program is OK, problem seems to be ppp.  You can bind to
a specific IP and its interface, but only if nothing else is already 
bound.  The following program successfully binds to the same port on
separate IP addresses and receives connections on the associated IP.
If it doesn't for you, then there's some problem with your platform -
but I assume you're running some kind of UNIX, given that you have
"lsof", and I find that even Linux gets this right.

If that checks out and lsof is right, then ppp bound to INADDR_ANY or
directly to this interface for some reason, and you can't have it.
If lsof shows it bound to *:port, then it bound to INADDR_ANY;  if
it bound directly, you'll see address:port.

	Donn Cave, donn at
import socket

import socket

sf = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sf.bind(('', 7017))  # substitute real IP 1.
sb = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sb.bind(('', 7017))  # substitute real IP 2.

print sf.accept()
print sb.accept()

More information about the Python-list mailing list