我用這個腳本Python的HTTP請求
from twisted.internet import reactor, threads
from urlparse import urlparse
import httplib
import itertools
concurrent = 200
finished=itertools.count(1)
reactor.suggestThreadPoolSize(concurrent)
def getStatus(ourl):
url = urlparse(ourl)
conn = httplib.HTTPConnection(url.netloc)
conn.request("HEAD", url.path)
res = conn.getresponse()
return res.status
def processResponse(response,url):
print response, url
processedOne()
def processError(error,url):
print "error", url#, error
processedOne()
def processedOne():
if finished.next()==added:
reactor.stop()
def addTask(url):
req = threads.deferToThread(getStatus, url)
req.addCallback(processResponse, url)
req.addErrback(processError, url)
added=0
for url in open('urllist.txt'):
added+=1
addTask(url.strip())
try:
reactor.run()
except KeyboardInterrupt:
reactor.stop()
當我嘗試運行腳本$蟒蛇test.py
它只是打印網址不做捲曲或發送HTTP請求..
我怎麼能發送的每一個
感謝
哪裏都是你的函數添加到'反應堆'? – dg123
@ user1436026你能解釋更多請 – SimpleojbC
爲什麼你在這裏使用'httplib'而不是Twisted的HTTP代碼?或者,如果你想爲每個連接使用'httplib'和一個線程,你爲什麼要使用Twisted並啓動一個反應堆? – abarnert