본문 바로가기
파이썬

파이썬 What is the fastest way to send 100,000 HTTP requests in Python?

by º기록 2020. 12. 5.
반응형


 

해결 방법

 

트위스트리스 솔루션 :

from urlparse import urlparse
from threading import Thread
import httplib, sys
from Queue import Queue

concurrent = 200

def doWork():
    while True:
        url = q.get()
        status, url = getStatus(url)
        doSomethingWithResult(status, url)
        q.task_done()

def getStatus(ourl):
    try:
        url = urlparse(ourl)
        conn = httplib.HTTPConnection(url.netloc)   
        conn.request("HEAD", url.path)
        res = conn.getresponse()
        return res.status, ourl
    except:
        return "error", ourl

def doSomethingWithResult(status, url):
    print status, url

q = Queue(concurrent * 2)
for i in range(concurrent):
    t = Thread(target=doWork)
    t.daemon = True
    t.start()
try:
    for url in open('urllist.txt'):
        q.put(url.strip())
    q.join()
except KeyboardInterrupt:
    sys.exit(1)

이것은 트위스트 솔루션보다 약간 빠르며 CPU를 덜 사용합니다.

 

참조 페이지 https://stackoverflow.com/questions/2632520

 

 

반응형

댓글