Examples and Tutorials of SocketServer?

  • Thread starter Milo K. Piffenpauffer
  • Start date
M

Milo K. Piffenpauffer

I've looked on Google for examples of using the SocketServer module,
but I'm not having much luck. The online documentation is pretty
sparse, and PyDoc isn't very clear. Does anyone know where I could
find an example of SocketServer that explains what the various
features of the code do?

Milo
 
P

Paul Rubin

I've looked on Google for examples of using the SocketServer module,
but I'm not having much luck. The online documentation is pretty
sparse, and PyDoc isn't very clear. Does anyone know where I could
find an example of SocketServer that explains what the various
features of the code do?

"Use the source, Luke". I remember having the same problem and
finding some better docs around somewhere though.
 
J

John E. Barham

Paul said:
"Use the source, Luke".

Ditto. Admittedly it can be confusing figuring out which of the methods you
need to override.

Here's a slightly edited minimal echo server from the printed Python
Cookbook (i.e., I couldn't find it in the Cookbook on the ActiveState
website):

import SocketServer

class EchoHandler(SocketServer.BaseRequestHandler):
def handle(self):
while 1:
data = self.request.recv(1024)
if not data: break
self.request.send(data)

EchoServer = SocketServer.TCPServer(("", 8881), EchoHandler)
EchoServer.serve_forever()

Run the above and see it in action by doing "telnet localhost 8881" and
typing away. The characters you type should be repeated (i.e., echoed). At
least that's what happens for me on XP...

Note that you don't even have to derive your server from one in
SocketServer. All you have to do minimally is sub-class the
BaseRequestHandler class and override the handle() method.

Note too that the standard library's SimpleHTTPServer is based on
SocketServer.TCPServer (via HTTPServer in BaseHTTPServer) so it's a good,
albeit more complex, example of a SocketServer server.

But at some point you should read and try to trace the source to
SocketServer. Really, it's not that complicated.

John
 
P

Paul Rubin

John E. Barham said:
But at some point you should read and try to trace the source to
SocketServer. Really, it's not that complicated.

It's actually pretty educational to see how such things are written.
The way the threading and forking mixins work is quite elegant.
 
F

Fredrik Lundh

Milo K. Piffenpauffer said:
I've looked on Google for examples of using the SocketServer module,
but I'm not having much luck. The online documentation is pretty
sparse, and PyDoc isn't very clear. Does anyone know where I could
find an example of SocketServer that explains what the various
features of the code do?

http://www.effbot.org/zone/librarybook-index.htm

chapter 7 might be of some help.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top