getting data from a port in use

Tony Meyer t-meyer at ihug.co.nz
Mon Jan 31 21:26:43 EST 2005


> I am trying to use Python to get the data received at a 
> specific port (in use) on my computer. I already tried below
> code which seems to hang at the statement accepting
> connections. 

Seems to hang, or does hang?  Using print statements will tell you whether
that's where it's getting stuck or not.

> I don't know what else I can try. Any
> suggestions will be welcome.
> 
> import socket, select, os 
> 
> PORT = 2005
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
> s.bind((socket.gethostname(), PORT))
> s.listen(1)
> work_socket, addr = s.accept() 
> data = s.recv(1024)
[...]

This should be 'data = work_socket.recv(1024)'.

This script works for me with that change.  (i.e. I can run it with port
2005 already in use, connect to the port, and it will finish without error).

=Tony.Meyer




More information about the Python-list mailing list