python twisted模块_Python学习---IO的异步[twisted模块]

安装twisted模块

Linux:pip3 install twisted

Window:a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted,

下载:Twisted-17.1.0-cp35-cp35m-win_amd64.whl

b. 进入文件所在目录

c. pip3 install Twisted-17.1.0-cp35-cp35m-win_amd64.whl

Twisted实例[小有问题]from twisted.web.client import getPage # 专门用来发送HTTP请求的

from twisted.web.client import defer

from twisted.internet import reactor # 循环等待用户请求的响应

def all_done(arg):

reactor.stop()

def callback(contents):

print(contents)

deferred_list = []

url_list = ['http://www.bing.com', 'http://www.baidu.com', ]

for url in url_list:

deferred = getPage(bytes(url, encoding='utf8')) # 加载url交给getPage()

deferred.addCallback(callback) # 回调函数表示请求完成后,需要做的操作

# deferred_list.append(deferred) #

# dlist = defer.DeferredList(deferred_list)

# dlist.addBoth(all_done)

reactor.run() # de

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值