安裝twisted模塊 html
Linux:python
pip3 install twisted
Window:react
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實例[小有問題]web
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