Getting a free TCP port & blocking it

T

theneb

Hi all,
I'm attempting to block a TCP port from any other application from
using it until I free it from python, this is so that:
1). Generate a random free user-space port
2). Generate the script for the external program with the port
3). Free the port before external program execution.

This is what I have so far:
class portFinder:
port = None
socketobj = None

def __init__(self):
i=2000
portStatus=0
while portStatus!=111:
sockobj = socket(AF_INET, SOCK_STREAM)
portStatus=sockobj.connect_ex(('localhost',i))
if portStatus!=111:
i+=1
sockobj.close()
self.socketobj=socket(AF_INET, SOCK_STREAM)
self.socketobj.bind(('localhost',i))
self.socketobj.setblocking(1)
self.port=i
def getPort(self):
return self.port

def free(self):
self.socketobj.close()
 
T

Tim Roberts

theneb said:
Hi all,
I'm attempting to block a TCP port from any other application from
using it until I free it from python, this is so that:
1). Generate a random free user-space port
2). Generate the script for the external program with the port
3). Free the port before external program execution.

What's the point? Why can't the actual user of the port create the port,
and then notify the other side of the port number?

And why don't you just specify a port number of 0 and let the system assign
you a free port number?
 
T

theneb

What's the point? Why can't the actual user of the port create the port,
and then notify the other side of the port number?
The system the app will run on will be creating three instances of the
external application, the python app has to keep track of which port
the external app is running on.
 
D

Dennis Lee Bieber

The system the app will run on will be creating three instances of the
external application, the python app has to keep track of which port
the external app is running on.
Still haven't addressed why the instances can't obtain the port on
their own, and send it out on some channel the parent can read
(subprocess/popen stdout/read, say). (or parent opens a port for the
instances to write /their/ ID/port information on)

Any attempt to have parent obtain a port/spawn an external program
with port as argument/release the port/external opens specified port is
going to be a candidate for a race condition.

1) if you don't specify REUSEADDR (or whatever that option really
in named), the port will likely be locked for some period of time after
the parent releases it... I think I've read of timeouts up to two
minutes before a port can be reused.

2) if you DO specify REUSEADDR, the OS is free to give that port to
the next task -- whatever it be, some background job doing a timeserver
synchronization, maybe -- that asks for a free port.

Much better to let the external program obtain whatever port number
it can and tell "you" what it is...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top