Re: a simple tcp server sample

2007-11-07 Thread Tzury Bar Yochay
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()

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


Re: a simple tcp server sample

2007-11-07 Thread Tzury Bar Yochay
> See the SocketServer module, both the documentation and the source code.

I firstly looked at this module and its __doc__, yet I still need an
'hello world' sample. and couldn't get it straight how can I write my
own hello world sample with SocketServer objects.


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


Re: a simple tcp server sample

2007-11-07 Thread Tzury Bar Yochay
> 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.

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


Re: a simple tcp server sample

2007-11-07 Thread Paul Rubin
Tzury Bar Yochay <[EMAIL PROTECTED]> writes:
> 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.

See the SocketServer module, both the documentation and the source code.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a simple tcp server sample

2007-11-07 Thread Jean-Paul Calderone
On Wed, 07 Nov 2007 18:54:39 -, Tzury Bar Yochay <[EMAIL PROTECTED]> 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
>
>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()

Even simpler, use Twisted:

from twisted.internet.protocol import ServerFactory, Protocol
from twisted.internet import reactor

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

class Echo(Protocol):
def connectionMade(self):
print 'Connection from', self.transport.getPeer()

def dataReceived(self, bytes):
self.transport.write(bytes)

factory = ServerFactory()
factory.protocol = Echo
reactor.listenTCP(HOST, PORT, factory)
reactor.run()

This doesn't have the bug your version has (where it randomly drops some
data on the floor) and handles multiple clients.

Check it out:  http://twistedmatrix.com/

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


Re: a simple tcp server sample

2007-11-07 Thread Erik Jones
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®
[EMAIL PROTECTED]
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


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


Re: a simple tcp server sample

2007-11-07 Thread Robert Hicks
On Nov 7, 1:54 pm, Tzury Bar Yochay <[EMAIL PROTECTED]> 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
>
> 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

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


a simple tcp server sample

2007-11-07 Thread Tzury Bar Yochay
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()

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