serious rresvport problem in python 2.2.1

Dan Stromberg strombrg at tesuji.nac.uci.edu
Tue Aug 13 01:32:26 CEST 2002


I've been using a modified version of Carey Evans' rresvport function
in a printsystem.

It worked fine in python 2.1.2 and python 2.1.3.

Unfortunately, it's not working out in 2.2.1.

I've included at the bottom, the rresvport module I've been using with
2.1.2 and 2.1.3.

The error I get is:

Traceback (most recent call last):
  File "./t", line 6, in ?
    s = rresvport.rresvport()
  File "./rresvport.py", line 41, in rresvport
    s.bind(('', port))
socket.gaierror: (3, 'getaddrinfo failed')


So I got kind of interested in that '' in the bind call.  This is
supposed to mean "localhost".  So I changed it to
bind(('localhost',port)).  This allows me to locate a port, but then a
subsequent connect() using said port gives:

   out_sock.connect((host,port))
socket.error: (126, 'Cannot assign requested address')


What do I need to get this working again?


Here's the rresvport module we've been using with 2.1.2 and 2.1.3:

# this one's from 
# Carey Evans  c.evans at clear.net.nz 
# http://mail.python.org/pipermail/python-list/1999-June/004387.html

import exceptions
import socket
import errno
import types

class RResvPortError(exceptions.Exception):
	def __str__(self):
		return 'No reserved ports available'

def rresvport():
	port = 1023
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	while port >= 1:
		print port
		try:
			# tuple for 2.1, probably non-tuple for pre-2.0
			s.bind(('', port))
		except socket.error,detail:
# This was in Carey's original code, but didn't work for me with python 2.1
# DRS.
#			if type(detail) is not types.TupleType \
#			   or detail[0] != errno.EADDRINUSE:
#				raise
# This is a working replacement.
## this used to work with 2.1.2 and 2.1.3, but doesn't with 2.2.1:
			if detail[0] != errno.EADDRINUSE:
				raise
#			pass
		else:
			return s
		port = port - 1

	raise RResvPortError


-- 
Dan Stromberg                                               UCI/NACS/DCS



More information about the Python-list mailing list