By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Socket sample?

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Daniel Orner wrote:
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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
Daniel Orner wrote:
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
Jul 18 '05 #4

P: n/a
Ah, I see! I didn't understand what those classes were doing. Thanks
very much!

--Daniel
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.