您的位置:首页 > 其它

twisted 使用

2016-05-13 21:28 316 查看
工欲善其事,必先利其器,我们先来进行 twisted 框架的安装,由于平时使用的都是 Windows 系统,那么下面我们就讲解下 Windows 下 twisted 框架的安装
(1)下载 twisted 框架,请根据你安装的 python 版本选中对应得到版本,下载地址:http://pan.baidu.com/s/1hsbJpas  下载之后打开即可安装,安装成功后,但在使用 twisted 时,即引入 twisted 时, 如:from twisted.python import log,运行时会提示 ImportError: Twisted requires zope.interface 3.6.0 or later: no module named
(2)下载 zope.interface-4.1.1-py2.7-win-amd64.egg,
  这是因为缺少 zope.interface,接下来安装 zope.interface,我选择的版本是4.1.1,其下载地址:http://pan.baidu.com/s/1hs8To6w,其他版本地址:https://pypi.python.org/pypi/zope.interface#downloads
下载完成后将 zope.interface-4.1.1-py2.7-win-amd64.egg 文件拷贝到 python 安装目录下的 Scripts文件夹下,安装 zope 包还需要下载安装 setuptools 工具,下载地址:http://pan.baidu.com/s/1slKgne9
安装完setuptools后可以看到Scripe中多了easy_install.exe,如图:




安装 setuptools 工具,进入解压目录执行:python setup.py install,安装完之后进入 C:\Python27\Scripts,执行:easy_install.exe zope.interface-4.1.1-py2.7-win-amd64.egg,即可完成 zope 包的安装,如图:




亲测自此,twisted安装完成。

下面是twisted的简单使用,其中的日志模块:

from twisted.python import log
from twisted.internet import defer

log.msg('This will not be logged, we have not installed a logger.')
log.startLogging(open(r"./test.log", 'ab'))
log.msg('Hello twisted.python.log!')
log.msg('This will be logged.')
log.err('This will be logged as an error.')
def bad_callback(result):
pass

try:
bad_callback()
except:
log.err('The next function call will log the traceback as an error.')
log.err()

d = defer.Deferred()

def on_error(failure):
log.err('The next function call will log the failure as an error.')
log.err(failure)

d.addCallback(bad_callback)
d.addErrback(on_error)
d.callback(True)
log.msg('End of example.')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: