开发者

Network programming in Python

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, M开发者_开发技巧ac OS X), and it only needs to be able to connect to other Python programs using the same library.


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.


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


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


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.


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.


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.


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.


Socket is low level api, it is mapped directly to operating system interface. Twisted, Tornado ... are high level framework (of course they are built on socket because socket is low level). When it come to TCP/IP programming, you should have some basic knowledge to make a decision about what you shoud use:

  • Will you use well-known protocol like HTTP, FTP or create your own protocol?
  • Blocking or non-blocking? Twisted, Tornado are non-blocking framework (basically like nodejs). Of course, socket can do everything because every other framework is base on its ;)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜