了解twisted时遇到的问题

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连接成功但服务器无响应
测试发现回调异常
查阅文档后暂定为版本问题,搁置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

丧心病狂の程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值