[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Getting a free TCP port & blocking it

theneb

3/1/2008 12:17:00 AM

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()
3 Answers

Tim Roberts

3/1/2008 7:12:00 AM

0

theneb <onetwofour@gmail.com> wrote:
>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?
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

theneb

3/1/2008 12:28:00 PM

0

On Feb 29, 11:11 pm, Tim Roberts <t...@probo.com> wrote:
> theneb <onetwof...@gmail.com> wrote:
> >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?
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.

>
> And why don't you just specify a port number of 0 and let the system assign
> you a free port number?
> --
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.

Dennis Lee Bieber

3/1/2008 6:49:00 PM

0

On Sat, 1 Mar 2008 04:28:22 -0800 (PST), theneb <onetwofour@gmail.com>
declaimed the following in comp.lang.python:

> On Feb 29, 11:11 pm, Tim Roberts <t...@probo.com> wrote:
> > 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.
>
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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/