socket examples

le dahut le.dahut at laposte.net
Tue Jan 24 06:39:54 EST 2006


I have a server that receives data from a client, detect the end of this 
data and send other data back to the client (it's to measure bandwidth).
I think the best way is to thread a part of the server's program am I 
right ?

HOST = socket.gethostname()
try:
     PORT = int(sys.argv[1])
     print PORT
except:
     PORT = 50009
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "s.bind((", HOST, PORT, "))"
s.bind((HOST, PORT))
s.listen(5)
while 1:
     conn, addr = s.accept()
     print 'Connected by', addr
     data=''
     while 1:
         data += conn.recv(1024)
         if data[-5:] == '#####': #the end of the data
             break

     data2='1'*1024*1024
     data2=data2[:-5]+'#####' #the end of the data
     conn.send(data2)
     conn.close()


Fredrik Lundh a écrit :
> "le dahut" wrote:
> 
> 
>>I've read the Gordon McMillan's "Socket Programming HOWTO" and I'm
>>looking for other documents that show examples on how to write a socket
>>server that can answer multiple clients at the same time.
>>Does someone know where I can find those documents/tutorials ?
> 
> 
> see
> 
>     http://effbot.org/tag/python.asyncore
>     http://effbot.org/tag/python.asynchat
> 
> or google for "twisted".
> 
> </F>
> 
> 
> 



More information about the Python-list mailing list