Re: Getting a free TCP port & blocking it

2008-03-01 Thread theneb
On Feb 29, 11:11 pm, Tim Roberts <[EMAIL PROTECTED]> wrote:
> theneb <[EMAIL PROTECTED]> 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, [EMAIL PROTECTED]
> Providenza & Boekelheide, Inc.

-- 
http://mail.python.org/mailman/listinfo/python-list


Getting a free TCP port & blocking it

2008-02-29 Thread 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()
-- 
http://mail.python.org/mailman/listinfo/python-list