Newbie question: how to keep a socket listening?

2005-06-24 Thread pwilkins
First off I'm want to learn socket/network programming with python
so a lot of what I ask is newbie related.

I have written a test socket server that runs as a daemon.
It listens on two sockets (say at ports 8000 and 9000) so that I can 
telnet over from another machine and get process info (ps type of info),
one one port and control the server on the other port.

What I want is to have the daemon run thread (A) which listens
on port 8000, and then runs another thread (B) which listens 9000.

Thread A monitors whether thread B is alive with a
thread_B_.isSet() call.
If thread B has terminated then thread A can exit when user tells
it to, otherwise not.

Thread B will terminate when user tells it to exit.

I can start the daemon and connect to both ports just fine.
If I leave the connections on using telnet for example, the connections
keep working (stay alive). This part works.

But I also want to be able to disconnect the telnet sessions,
but leave the daemon server still listening on both ports so I can
reconnect to both ports later.

If I disconnect and try to reconnect within about 10secs it works fine.
However if I stay disconnected from more than a minute then
I cannot reconnect later. It seems as if the the server is not
listening anymorealthough the threads are still running
i.e. the daemon is up.

The message from telnet is:
>>telnet: connect to address xxx.xxx.xxx.xxx: Connection refused

Perhaps I do not understand the function of the following:
socket.setdefaulttimeout(15)
socket.settimeout(15.0)

What is the difference between these??
Yes I have read the python docs
 - setdefaulttimeout(timeout) Set the default timeout in floating
   seconds for new socket objects.
 whereas:
 - settimeout(value): Set a timeout on blocking socket operations.

So what this means to me is that with setdefaulttimeout one can set
a global timeout for new sockets, wheres with settimeout one can fine tune
it after a socket has been created. This is probably all wrong.

Any ideas as to what is causing the server to stop listening after I disconnect
for a long period of time.

Also does one have to do a socket.shutdown() before one does a socket.close??

How should a server disconnect a client but keep listening for subsequent
connections from other clients?

I have included a summary of important code below.
Let me know if should provide more or different info.

Thanks a lot in advance.
Pete

-
The program basically works as below:
-
(1) become a daemon

(2) call server function which does the following (summarized):
import socket
...

HOST = ''

socket.setdefaulttimeout(15)

try:
sa = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sa.bind((HOST, AdminPort))
sa.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sa.settimeout(15.0)
sa.listen(1)
except:
## exit on failure

## run 2nd socket handler as thread
# create an event to signal the completion of client handler thread 
client_handler_complete=threading.Event()
client_handler_complete.clear()

server_handler_thread = threading.Thread (None, ClientHandler, None, \
(StateValues, client_handler_complete))
try:
server_handler_thread.start()
except:
## exit on failure

while 1:## wait for a connection
try:
#...waiting for connection
(client, address)=sa.accept()
except sa.timeout:
#...waiting for connection timeout
continue
except:
continue## for now ignore this!

#...someone's connecting, so check if ipaddress is allowed
remote = str(address[0])
if Allowed_Hosts.has_key(remote):
hostname = Allowed_Hosts[remote]
Message = '%s connection accepted from: %s (%s:%s))' % \
(FUNCNAME, hostname, address[0], address[1])
log_message(StateValues, Message, 1)
else:
client.close()
Message = '%s connection rejected from: %s' % 
(FUNCNAME, address)
log_message(StateValues, Message, 1)
continue

socketfile = client.makefile()

while 1: ## wait for user input
data = ''
data=read_data(socketfile)
if not data or data == 'CR':
continue
else:
Message = '%s input received: %s' % (FUNCNAME, 
data)
 

Re: Newbie question: how to keep a socket listening?

2005-06-24 Thread pwilkins
On Fri, 24 Jun 2005 13:59:19 -0400, pwilkins wrote:

>   if data == 'q':
>##disconnect client but keep waiting for connections
>   ...
>   client.close()

Sorry - made a mistake in my posting...
the above should have been:

if data == 'q':
 ##disconnect client but keep waiting for connections
 ...
 client.close()
 break # return to the wait for connections while loop

Again thanks...

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


Re: Newbie question: how to keep a socket listening?

2005-06-24 Thread pwilkins
On Fri, 24 Jun 2005 11:21:28 -0700, ncf wrote:

> I think your problem /may/ be in the following line of code:
> sa.listen(1)
> 
> I believe what's happening is that the listen() creates a decremental
> counter of the number of connections to accept. Once it decrements to
> 0, it won't accept any more connections. (I'm not at all sure, but that
> sounds like what it's doing.)
Good point, will read up on socket.listen(x) 

> If you find out the problem, please post it up here for all to learn
> from :)
Will do that
> 
> Oh, and just a suggestion, do something like a 'netstat -a -p --inet' on
> the daemon end. (Might not be the completely correct command =\)

I did that is how I know the ports are still begin held
thx, pete

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


Re: Newbie question: SOLVED (how to keep a socket listening), but still some questions

2005-06-24 Thread pwilkins
On Fri, 24 Jun 2005 21:42:48 -0400, Jp Calderone wrote:


> shutdown actually tears down the TCP connection; close releases the file
> descriptor.
> 
> If there is only one file descriptor referring to the TCP connection,
> these are more or less the same.  If there is more than one file
> descriptor, though, the difference should be apparent.
> 
> Jp


Yes I think you are right. I checked with the "Socket Programming HOWTO"
by Gordon McMillan @ http://www.python.org/doc/howto.

Here's a quote:
"Strictly speaking, you're supposed to use shutdown on a socket before you
close it. The shutdown is an advisory to the socket at the other end.
Depending on the argument you pass it, it can mean "I'm not going to send
anymore, but I'll still listen", or "I'm not listening, good riddance!".
Most socket libraries, however, are so used to programmers neglecting to
use this piece of etiquette that normally a close is the same as
shutdown(); close(). So in most situations, an explicit shutdown is not
needed."

However my first understanding from the python docs is quite different..
it was that like a unix pipe, a socket could be made to communicate in one
direction only. Like a RD-only pipe a socket could be made RD-only and
another socket WR-only...perhaps like ftp keeps the control channel open
but transmits over a read only socket???



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