Twisted安装:
Pip install twisted
显示
Requirement already satisfied: twisted in e:\python\lib\site-packages
但是编译过程中
ImportError: No module named twisted.internet
在python环境下使用 help(‘modules’)
能够看到twisted模块
尝试卸载该模块 pip uninstall twisted
后用pip重新安装,成功。
callWhenRunning:
reactor.callWhenRunning(a = fibonacci(1,1,99))
这个语句出现错误
TypeError: callWhenRunning() takes at least 2 arguments (1 given)
去掉赋值语句,通过函数内部赋值
错误:
AssertionError: 573147844013817084101 is not callable
去掉递归语句
错误:
AssertionError: None is not callable
检查发现函数引用不应有()
重新测试成功
用于测试的递归函数(计算斐波那契数列):
def fibonacci(a,b,n):
if n:
return fibonacci(b,a+b,n-1)
else:
return b
通过traceback观察Twisted调用:
File “test.py”, line 14, in
reactor.run() File “E:\python\lib\site-packages\twisted\internet\base.py”, line 1242, in
run
self.startRunning(installSignalHandlers=installSignalHandlers) File “E:\python\lib\site-packages\twisted\internet\base.py”, line
1222, in startRunning
ReactorBase.startRunning(self) File “E:\python\lib\site-packages\twisted\internet\base.py”, line 735, in
startRunning
self.fireSystemEvent(‘startup’) File “E:\python\lib\site-packages\twisted\internet\base.py”, line 686, in
fireSystemEvent
event.fireEvent() File “E:\python\lib\site-packages\twisted\internet\base.py”, line 428, in
fireEvent
DeferredList(beforeResults).addCallback(self._continueFiring) File “E:\python\lib\site-packages\twisted\internet\defer.py”, line
321, in addCallback
callbackKeywords=kw) File “E:\python\lib\site-packages\twisted\internet\defer.py”, line 310, in
addCallbacks
self._runCallbacks() File “E:\python\lib\site-packages\twisted\internet\defer.py”, line 653, in
_runCallbacks
current.result = callback(current.result, *args, **kw) File “E:\python\lib\site-packages\twisted\internet\base.py”, line 441, in
_continueFiring
callable(*args, **kwargs) File “test.py”, line 11, in ask traceback.print_stack()
stop():
通过在程序内部使用reactor.stop()
终止
addReader(self):
创建一个socket连接后,通过reactor.addReader(self)
将自己传递给reactor
ClientFactory和Protocol:
ClientFactory的工作是管理连接事件,Protocol对象处理为一个成功的连接。一旦连接建立,Protocol就接管下面的工作,包括收发数据和决定是否关闭连接。
当编写一个启动非同步操作的函数时,返回一个Deferred对象。当操作完成时,调用Deferred的callback方法来返回值。如果操作失败,调用Deferred.errback函数来跑出异常。
临时文件下载网页:
downloadToTempFile(url).addCallback(printFile).addErrback(printError)
reactor.run()
def downloadToTempFile(url):
tmpfd,tempfilename = tempfile.mkstemp()
os.close(tmpfd)
return client.downloadPage(url,tempfilename).addCallback(returnFilename,tempfilename)
实现一个简单的http服务器:
from twisted.web import http
class MyHttpFactory(http.HTTPFactory):
protocal = MyHttp
class MyHttp(http.HTTPChannel):
requestFactory = MyRequestHandler
class MyRequestHandler(http.Request):
pages = {
'/':'<h1>Home</h1>Home Page',
'/test':'<h1>Test</h1>Test Page',
}
def process(self):
if self.pages.has_key(self.path):
self.write(self.pages[self.path])
else:
self.setResponseCode(http.NOT_FOUND)
self.write("<h1>Not Found</h1>Sorry,no such page.")
self.finish()
if __name__ == '__main__':
from twisted.internet import reactor
reactor.listenTCP(8000,MyHttpFactory())
reactor.run()
出现错误代码:
NameError: name ‘MyHttp’ is not defined
调整类声明顺序
再次测试,在浏览器输入地址http://127.0.0.1:8080/无响应
检查浏览器network,发现tcp连接成功但服务器无响应
测试发现回调异常
查阅文档后暂定为版本问题,搁置