Socket Error : Address still in use (Conveting from python 1.5.2 to 2.7.1)

Wong Wah Meng-R32813 r32813 at freescale.com
Tue Mar 27 11:41:35 CEST 2012


Hello there,

I am in the midst of converting my application from python 1.5.2 to python 2.7.1 on HP-UX 11 Itanium box. 

My application server will set a listening port, accepting request from multiple clients. The code just works fine in the old python environment. E.g. when I do a lsof | grep <listening port> I got the following.

python     62602  genasm    5u  IPv4 0x7350d1f0        0t0  TCP zmy02aix02:12121 (LISTEN)
python     62602  genasm    6u  IPv4 0x744fb5f0        0t0  TCP zmy02aix02:12121->zmy02aix02-bkup:51867 (ESTABLISHED)
python     62602  genasm    7u  IPv4 0x75b959f0        0t0  TCP zmy02aix02:12121->zmy02aix02-bkup:51869 (ESTABLISHED)
python     62602  genasm    8u  IPv4 0x75a559f0        0t0  TCP zmy02aix02:12121->zmy02aix02-bkup:51873 (ESTABLISHED)

Strange things happened in python 2.7.1. Without modifying the code of how the socket was created and how the TCP/IP address was bound to the socket, it seems that every other processes that I run, which supposed to connect to the listening port as a client program, also appears to be holding a listening port. This is weird. Anyone has encountered this before especially when you were converting from an old python to a new python? Like you can see below there are 5 processes hosting the listening port of 18882.

$ lsof -i tcp | grep 18882
python  10598 r32813    3u  IPv4 0xe00000050b73e400       0t0  TCP zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  18181 r32813    3u  IPv4 0xe00000050b73e400       0t0  TCP zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  20025 r32813    3u  IPv4 0xe00000050b73e400       0t0  TCP zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  26295 r32813    3u  IPv4 0xe00000050b73e400       0t0  TCP zmy02hp3.ap.freescale.net:18882 (LISTEN)
python  26428 r32813    3u  IPv4 0xe00000050b73e400       0t0  TCP zmy02hp3.ap.freescale.net:18882 (LISTEN)

Since only one of them is the genuine process holding the port, I need to kill off the rest of the process if I need to restart the genuine process running under that port. It should not work this way. 

Here is the code of the application process that hosts the listening port. 

	self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
	self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
	self.sock.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1
	self.sock.setsockopt( socket.IPPROTO_TCP, _TCP_NODELAY, 1 )
	self.sock.bind( self.server_address )
  

Here is the client code that does the connection.

       self.sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
       self.sock.setsockopt( socket.IPPROTO_TCP, _TCP_NODELAY, 1 )
       self.sock.connect( self.server_address )

Regards,
Wah Meng



More information about the Python-list mailing list