Re: [Tutor] Question about network server in python

2006-06-15 Thread Kent Johnson
Tino Dai wrote:
> Hi there,
> 
>   I am wondering if somebody to could answer a question about 
> sockets. I have a socket that
> is listening, and a client program connects to it. The client program 
> transfers a name over, and then disconnects from the socket. Now, how 
> that is done is using a socket.close() call to shut down the entire 
> socket. My question is: Is there a way to have the socket close the 
> connection, yet stay open for the next client that comes along and 
> connects to the server? I have my already written code below for further 
> documentation. Thanks!
> 
> while 1:
>   s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>   s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
>   s.bind((self.ipAddr,self.port))
>   s.listen(5)
>   print "The port is: ", self.port   
>   client,addr=s.accept()
>   while 1:
>try:
>if addr[0] == self.remoteIpAddr:
>client.send("Connected to the server\n")   
>msg=client.recv(1024)
>msg=msg.strip()
>if msg in 'exit':
> s.close()  #Is there a different 
> way to write this?

I think you want to close the client socket - client.close() - rather 
than the master socket.

You might be interested in the SocketServer library which helps to write 
simple socket servers such as this. One advantage of using SocketServer 
is it makes it trivial to convert your server to a threaded or forked 
server. Here are some examples:
http://www.amk.ca/python/simple/fingerd.py.html
http://examples.oreilly.com/pythonian/ see example 19-5

Kent

> time.sleep(30)
> print "exiting"
> break
>if len(msg) > 0:
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Kent Johnson
Peter Jessop wrote:
> I think the problem here is the 'break' statement.
> Does it not put you outside the while loop whereas in order to keep
> the server socket open you need it to loop forever.
> 
> I also think that the s.accept should be inside the while loop.

There are two loops, I think you missed the outer loop.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Peter Jessop
import socket
host = ''
port = 57000
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(5)
while 1:
client,addr=s.accept()
client.send("Connected to the server\n")
#if someCondition:
 #   cliente.close()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about network server in python

2006-06-15 Thread Peter Jessop
I think the problem here is the 'break' statement.
Does it not put you outside the while loop whereas in order to keep
the server socket open you need it to loop forever.

I also think that the s.accept should be inside the while loop.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor