Last active
November 15, 2018 01:44
-
-
Save tongtie/7e783af933f66f33fc13c5526e2cc305 to your computer and use it in GitHub Desktop.
Fast Send Http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# copy from stack overflow | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment