Socket sample?

D

Daniel Orner

I'm a newcomer to socket programming... I need to use a server socket
which should be able to handle more than one connection at a time. I've
read the HOWTO, but I'm still a bit confused about some of the more
intimate details. I've also seen a few pieces of sample code scattered
here and there, but nothing substantial.
Does anyone know of some sample code that handles multiple connections
(either by using select() or forking/threading processes)?
Thanks very much!

--Daniel
 
D

Diez B. Roggisch

Daniel said:
I'm a newcomer to socket programming... I need to use a server socket
which should be able to handle more than one connection at a time. I've
read the HOWTO, but I'm still a bit confused about some of the more
intimate details. I've also seen a few pieces of sample code scattered
here and there, but nothing substantial.
Does anyone know of some sample code that handles multiple connections
(either by using select() or forking/threading processes)?
Thanks very much!

use twisted - its well worth the effort, and supports the things you need.
 
D

Daniel Orner

Hmm... I'm looked at the SocketServer, but I'm pretty confused about
how to use it. As far as I can tell it doesn't actually do any
forking/threading/selecting at all. There's a lot of generic stuff which
seems to allow that behavior for subclasses, but I can't see any
specific code that does that kind of thing. Or maybe I'm just missing
something stupendously obvious. If so, would you be so kind as to give
me a quick example of how it would be used?
I also took a quick look at twisted... it looks like it's way too
complex for what I have in mind. -_- Basically, I just want a socket to
transfer information to/from a client program, essentially to
synchronize data between the server and the client. The program is run
automatically (no user input). However, I do want it to be fast, which
means no waiting for a connection. That's why I just want to see some
sample code about how to use a server socket.
Any help would be greatly appreciated!

--Daniel
 
I

Irmen de Jong

Daniel said:
Hmm... I'm looked at the SocketServer, but I'm pretty confused about
how to use it. As far as I can tell it doesn't actually do any
forking/threading/selecting at all. There's a lot of generic stuff which
seems to allow that behavior for subclasses, but I can't see any
specific code that does that kind of thing. Or maybe I'm just missing
something stupendously obvious. If so, would you be so kind as to give
me a quick example of how it would be used?

The SocketServer docs say:
"The solution is to create a separate process or thread to handle each request; the
ForkingMixIn and ThreadingMixIn mix-in classes can be used to support asynchronous
behaviour. "

And if you look in SocketServer.py (from python 2.3.3) you'll see at line 479:

class ForkingUDPServer(ForkingMixIn, UDPServer): pass
class ForkingTCPServer(ForkingMixIn, TCPServer): pass

class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
class ThreadingTCPServer(ThreadingMixIn, TCPServer): pass


So it already defines 4 specialized server classes for you to use :)
Threading, or forking, whatever suits you best.
If you want a TCP server that uses a new thread for each request,
just use SocketServer.ThreadingTCPServer instead of
SocketServer.TCPServer and you're done!

--Irmen
 
D

Daniel Orner

Ah, I see! I didn't understand what those classes were doing. Thanks
very much!

--Daniel
 

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

Similar Threads


Members online

Forum statistics

Threads
474,199
Messages
2,571,045
Members
47,643
Latest member
ashutoshjha_1101

Latest Threads

Top