views:

327

answers:

7

What library should I use for network programming? Is sockets the best, or is there a higher level interface, that is standard?

I need something that will be pretty cross platform (ie. Linux, Windows, Mac OS X), and it only needs to be able to connect to other Python programs using the same library.

+6  A: 

There is a framework that you may be interested in: Twisted

Jeramy Rutley
+4  A: 

A lot of people like Twisted. I was a huge fan for awhile, but on working with it a bit and thinking about it more I've become disenchanted. It's complex, and last I looked, a lot of it had the assumption that your program would always be able to send data leading to possible situations in which your program grows memory usage endlessly buffering data to send that isn't being picked up by the remote side or isn't being picked up fast enough.

In my opinion, it depends a lot on what kind of network programming you want to do. A lot of times you don't really care about getting stuff done while you're waiting for IO. HTTP, for example, is very request-response oriented, and if you're only talking to a single server there is little reason to need something like Twisted and plain sockets or Python's built-in HTTP libraries will work fine.

If you're writing a server of any kind, you almost certainly need to be event-driven. Twisted has a slight edge there, but it still seems overly complex to me. Bittorrent, for example, was written in Python and doesn't use Twisted at all.

Another factor favoring Twisted is that there is code for a lot of protocols already written for it. So if you want to speak an existing protocol a lot of hard work may already have been done for you.

Omnifarious
+5  A: 

As previously mentioned, Twisted is the most popular (by far). However, there are a lot of other alternative worth exploring. Tornado and Diesel are probably the top two contenders. A more complete comparison is found here.

jamieb
If you add a link to Twised I can consider your answer to be better than Jeramy's.
Omnifarious
+5  A: 

the answer depends on what you are trying to do.

"What library should I use for network programming?" is pretty vague.

for example, if you want to do HTTP, you might look at such standard libraries as urllib, urllib2, httplib, sockets. It all depends on which protocol you are looking to use, and which network layer you want to work at.

there are libraries in python for various network tasks... email, web, rpc, etc etc...

for starters, look over the standard library reference manual and see which tasks you want to do, then go from there: http://docs.python.org/library/index.html

Corey Goldberg
+5  A: 

Personally I just use asyncore from the standard library, which is a bit like a very cut-down version of Twisted, but this is because I prefer a simple and low level interface. If you want a higher level interface, especially just to communicate with another instance of your own program, you don't necessarily have to worry about the networking layer, and can consider something higher level like RPyC or pyro instead. The network then becomes an implementation detail and you can concentrate on just sending the information.

Kylotan
A: 

The socket module in the standard lib is in my opinion a good choice if you don't need high performance.

It is a very famous API that is known by almost every developpers of almost every languages. It's quite sipmple and there is a lot of information available on the internet. Moreover, it will be easier for other people to understand your code.

I guess that an event-driven framework like Twisted has better performance but in basic cases standard sockets are enough.

Of course, if you use a higher-level protocol (http, ftp...), you should use the corresponding implementation in the python standard library.

luc
+4  A: 

You just want to send python data between nodes (possibly on separate computers)? You might want to look at SimpleXMLRPCServer. It's based on the inbuilt HTTP server, which is based on the inbuilt Socket server, neither of which are the most industrial-strength servers around, but it's easy to set up in a hurry:

from SimpleXMLRPCServer import SimpleXMLRPCServer
server = SimpleXMLRPCServer(("localhost", 9876))

def my_func(a,b):
    return a + b

server.register_function(my_func)
server.serve_forever()

And easy to connect to:

import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:9876')

print s.my_func(2,3) 
>>> 5
print type(s.my_func(2,3))
>>> <type 'int'>
print s.my_func(2,3.0):
>>> 7.0

Twisted is popular for industrial applications, but it's got a brutal learning curve.

wisty
+1 for being another person who realises that the OP doesn't necessarily want networking, just communications.
Kylotan