標籤:tab odi bing com import lib size bytes reactor
安裝twisted模組
Linux:
pip3 install twisted
Window:
a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted,下載:Twisted-17.1.0-cp35-cp35m-win_amd64.whlb. 進入檔案所在目錄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 deferfrom 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
Python學習---IO的非同步[twisted模組]