bind problem

Dan Stromberg strombrg at tesuji.nac.uci.edu
Fri Nov 15 12:40:13 EST 2002


I'm still having my previously-mentioned problem binding sockets in
python 2.2.  Things are fine on linux, but they don't work on solaris
- some solaris hosts, not all, I think.

I'm approaching the group again about it, because I've run across
another, much smaller program with the same problem - in fact it's so
small, but I think it'll be ok to post it here in its entirety.

The error I get with 2.2.1 is:

Traceback (most recent call last):
  File "/tmp/packetpasser", line 108, in ?
    main()
  File "/tmp/packetpasser", line 60, in main
    sockin.bind(('',read_from))
socket.gaierror: (3, 'getaddrinfo failed')

I call the program "packetpasser", because it just passes packets from
one host to another.

Is there anyone else out there using sockets on solaris via python?

The code is:

# doesn't work
#!/dcs/packages/python-2.2.1/bin/pyhome

# works
#!/dcs/packages/python-2.1.3/bin/pyhome

# works
#!/dcs/packages/python-2.1.2/bin/pyhome

# works
#!/dcs/packages/python-1.5/bin/pyhome

# pyhome is just something that sets $PYTHONHOME, and then execs
# python.  It facilitates having more than one version of python on
# the same system in some situations.

# this didn't work on sol2, with early versions of python.  It seems fine
# with 1.0.3

import sys
import posixpath
import socket
import SOCKET
import select
import re
import time
import string

BUFSIZE=4096

def portno(p):
	numeric=re.compile('^[0-9][0-9]*$')
	if not numeric.match(p):
		return socket.getservbyname(p,'tcp')
	else:
		return string.atoi(p)
	
def main():
	verbose=0
	if sys.argv[1:] and sys.argv[1] == '-v':
		verbose=1
		del sys.argv[1]
	bogus_usage=0
	if sys.argv[2:]:
		read_from = portno(sys.argv[1])
		write_on = portno(sys.argv[2])
	else:
		bogus_usage=1
	if sys.argv[3:]:
		host=sys.argv[3]
	else:
		host='localhost'
	if bogus_usage:
		sys.stderr.write('usage: packetpasser input_socket output_socket output_host\n')
		sys.exit(1)
	if verbose:
		print 'input port:',read_from
		print 'output port:',write_on
		sys.stdout.flush()
	sockin = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	sockin.setsockopt(SOCKET.SOL_SOCKET,SOCKET.SO_REUSEADDR,1)
	sockin.bind(('',read_from))
	sockin.listen(0)
	while 1:
		if verbose:
			print 'waiting for connection...'
			sys.stdout.flush()
		connin, (remotehost, remoteport) = sockin.accept()
		if verbose:
			print 'connection from',remotehost,remoteport
			sys.stdout.flush()
		sockout = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		sockout.connect((host,write_on))
		contin = 1
		while contin:
			# wait until Something is ready
			(readyin,dummy2,exc) = select.select([connin,sockout],[],[],None)
			# then read it
			for i in readyin:
				if i == sockout:
					data = sockout.recv(BUFSIZE)
					if len(data) == 0:
						if verbose:
							print 'empty packet from sockout, exiting'
							sys.stdout.flush()
						contin = 0
					else:
						if verbose:
							print 'from dest ('+`len(data)`+')',data
							sys.stdout.flush()
						connin.send(data)
				if i == connin:
					data = connin.recv(BUFSIZE)
					if len(data) == 0:
						if verbose:
							print 'empty packet from connin, exiting'
							sys.stdout.flush()
						contin = 0
					else:
						if verbose:
							print 'from source ('+`len(data)`+')',data
							sys.stdout.flush()
						sockout.send(data)
			if len(exc) != 0:
				sys.stderr.write('exception\n')
				sys.exit(1)
		connin.shutdown(2)
		sockout.close()

main()



Anyone have any suggestions?
-- 
Dan Stromberg                                               UCI/NACS/DCS



More information about the Python-list mailing list