468,549 Members | 2,050 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

a simple tcp server sample

hi, the following sample (from docs.python.org) is a server that can
actually serve only single client at a time.

In my case I need a simple server that can serve more than one client.
I couldn't find an example on how to do that and be glad to get a
hint.

Thanks in advance

import socket

HOST = '127.0.0.1' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

print 'Connected by', addr

while 1:
data = conn.recv(1024)
if not data:
break
conn.send(data)

conn.close()

Nov 7 '07 #1
5 25462
On Nov 7, 1:54 pm, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
hi, the following sample (from docs.python.org) is a server that can
actually serve only single client at a time.

In my case I need a simple server that can serve more than one client.
I couldn't find an example on how to do that and be glad to get a
hint.

Thanks in advance

import socket

HOST = '127.0.0.1' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()

print 'Connected by', addr

while 1:
data = conn.recv(1024)
if not data:
break
conn.send(data)

conn.close()
POE

Nov 7 '07 #2
On Nov 7, 2007, at 12:54 PM, Tzury Bar Yochay wrote:
hi, the following sample (from docs.python.org) is a server that can
actually serve only single client at a time.

In my case I need a simple server that can serve more than one client.
I couldn't find an example on how to do that and be glad to get a
hint.

Thanks in advance
That's when it stops being simple. You'll need to spawn threads or
fork off separate processes to do that.

Erik Jones

Software Developer | Emmaź
er**@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com
Nov 7 '07 #3
On Wed, 07 Nov 2007 13:42:18 -0600, Erik Jones wrote:
On Nov 7, 2007, at 12:54 PM, Tzury Bar Yochay wrote:
>hi, the following sample (from docs.python.org) is a server that can
actually serve only single client at a time.

In my case I need a simple server that can serve more than one client.
I couldn't find an example on how to do that and be glad to get a hint.

Thanks in advance

That's when it stops being simple. You'll need to spawn threads or fork
off separate processes to do that.

I think the authors of Twisted would be very surprised to find out that
their framework can only serve one client at a time.

:)

Carl Banks
Nov 7 '07 #4
Even simpler, use Twisted:

I am afraid Twisted is not the right choice in my case. I am looking
for smaller, simpler and minimal server sample.

Nov 8 '07 #5
here is its:
# a simple tcp server

import SocketServer
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')

def handle(self):
while 1:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return

def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')

#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50008),
EchoRequestHandler)
server.serve_forever()

Nov 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by MBW | last post: by
1 post views Thread by Eric Law | last post: by
7 posts views Thread by Mihajlo Cvetanovic | last post: by
reply views Thread by Link | last post: by
reply views Thread by zenBen | last post: by
3 posts views Thread by sathish.sarod | last post: by
reply views Thread by boazin | last post: by
6 posts views Thread by Joe | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.