Twisted简单安装和rpc简单使用示例
twisted-doc-zh(Twisted中文文档项目)挪到 Launchpad 上去了,详情请见:
http://contrib.exoweb.net/wiki/TwistedDocZh
另外,有个翻译《Twisted Network Programming Essentials》(《Twisted网络编程必备》)
http://blog.csdn.net/gashero/category/277510.aspx
Twisted简单安装和rpc简单使用示例
from:http://wiki.huihoo.com/wiki/Twisted
Code:
http://twistedmatrix.com/trac/wiki/Downloads wget http://tmrc.mit.edu/mirror/twisted/Twisted/2.5/Twisted-2.5.0.tar.bz2 bunzip2 Twisted-2.5.0.tar.bz2 tar xvf Twisted-2.5.0.tar cd Twisted-2.5.0 cd zope.interface-3.3.0 python setup.py install cd .. python setup.py install $ trial twisted // Trial is Twisted's testing framework. >>> from twisted.web.client import getPage >>> from twisted.internet import reactor >>> def printContents(contents): print "The Deferred has called printContents with the following contents:" print contents reactor.stop() >>> deferred = getPage('http://www.huihoo.com/') >>> deferred.addCallback(printContents) <Deferred at 0xb9afd0> >>> reactor.run() |
Creating XML-RPC server
Server: touch twisted-xmlrpc.py
Code:
from twisted.web import xmlrpc, server class Example(xmlrpc.XMLRPC): """An example object to be published.""" def xmlrpc_echo(self, x): """Return all passed args.""" return x def xmlrpc_add(self, a, b): """Return sum of arguments.""" return a + b if __name__ == '__main__': from twisted.internet import reactor r = Example() reactor.listenTCP(7080, server.Site(r)) reactor.run() |
python twisted-xmlrpc.py
Client: run client
Code:
>>> from twisted.web import xmlrpc, server >>> import xmlrpclib >>> s = xmlrpclib.Server('http://localhost:7080/') >>> s.echo('huihoo') 'huihoo' >>> s.add(1,2) 3 |