Binding to specific IP addresses

Graham Ashton graz at mindless.com
Thu Aug 23 11:55:49 CEST 2001


[Having just written this post I've realised that to summarise, I might
just be asking "how do I avoid binding a socket to all interfaces?".
Maybe.]

I've got a problem with an application that uses a subclass of 
SocketServer.TCPServer. It doesn't seem to behave itself when binding 
sockets to IP addresses.

Here is a snippet of code:

  print "binding to %s:%d" % (self.server_ip, self.server_port)
  self.server = MyTCPServer((self.server_ip, self.server_port), MyHandler)

The self.server_ip is set to "192.168.5.1", rather than the empty string,
which I understand to mean that the server will only listen for inbound
requests on that IP address, not on my dial up PPP connection.

MyTCPServer (being a subclass of SocketServer.TCPServer) calls
self.server_bind() from it's __init__() method. After the code above has
been run I get a traceback:

ratchet% ./landiallerd.py 
binding to 192.168.5.1:6543
Traceback (most recent call last):
  File "./landiallerd.py", line 176, in ?
    app.run()
  File "./landiallerd.py", line 100, in run
    self.server = MyTCPServer((self.server_ip, self.server_port), MyHandler)
  File "./landiallerd.py", line 42, in __init__
    RequestHandlerClass)
  File "/usr/local/lib/python2.1/SocketServer.py", line 322, in __init__
    self.server_bind()
  File "/usr/local/lib/python2.1/SocketServer.py", line 333, in server_bind
    self.socket.bind(self.server_address)
socket.error: (98, 'Address already in use')

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 192.168.5.1
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?

Thanks.

--
Graham

P.S. I've been through the self.allow_reuse_address = 1 thing already, so
that shouldn't be the problem.



More information about the Python-list mailing list