Socket question: arbitrary port

Mike Fletcher mfletch at tpresence.com
Thu Jul 27 23:23:52 EDT 2000


Oh, dang, turns out I'm a lazy person, mama'll be so disappointed :o) !

I'm wondering about the question though, and if it might be asking how to
connect to a service that might be on multiple ports (port scanning)...
there are cases where I've created port ranges (such as the MUVRML reserved
port-range) in which services can exist.  The thing is that Python alone
_can't_ determine if the correct protocol is being used on the server (from
the client (well, assuming we're talking something based on UDP/TCP)), so
you have to do "application-level" testing to determine if you've got a
usable connection (keeping in mind that in these cases you'll likely find
_other_ services running on those "conflicted" ports).

If it was a question related to binding (as you supposed), then the trying
each port approach was the best I came up with (seems to work, I have a
hundred clients on the same machine able to correctly negotiate port usage
among themselves with no perceptible delay (note, these are UDP ports)).

Enjoy,
Mike

-----Original Message-----
From: Robert W. Bill [mailto:rbill at digisprings.com]
Sent: Thursday, July 27, 2000 2:37 PM
To: python-list at python.org
Subject: Re: Socket question: arbitrary port



On Thu, 27 Jul 2000, Jean Bergeron wrote:

> Is it possible to connect a socket through an arbitrary port?
> In other words, can Python find an available port without having the
> programmer (or user) specify a particular port number?
> 
> Any help would be appreaciated!
> Thanks!
> 
...
portNum = 1
while 1:
    try: 
	s.bind('127.0.0.1', portNum)
	break
    except socket.error, why:
	# could analyze why here 
	portNum = portNum + 1
...




More information about the Python-list mailing list