Skip to content

Instantly share code, notes, and snippets.

@tongtie
Last active November 15, 2018 01:44
Show Gist options
  • Save tongtie/7e783af933f66f33fc13c5526e2cc305 to your computer and use it in GitHub Desktop.
Save tongtie/7e783af933f66f33fc13c5526e2cc305 to your computer and use it in GitHub Desktop.
Fast Send Http
# 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