#!/usr/bin/env python
import httplib
import sys
from threading import Thread
from urlparse import urlparse
from Queue import Queue
import datetime

# import MySQLdb

start_time=datetime.datetime.now()


concurrent = 500


def doWork():
    while True:
        url = q.get()
        url ="http://ka_distributors.retroerp.com/api.php"
        status, url = getStatus(url)
        doSomethingWithResult(status, url)
        q.task_done()


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


def doSomethingWithResult(status, url):
    file = open('newfile.txt', "a+")
    file.write(str(status)+"\n")
    file.close()

q = Queue(concurrent * 2)
for i in range(concurrent):
    t = Thread(target=doWork)
    t.daemon = True
    t.start()
try:
    # db = MySQLdb.connect(host="localhost",
    #                      user="root",
    #                      passwd="creative",
    #                      db="potful_dev")
    #
    # cursor = db.cursor()
    # cursor.execute("SELECT * FROM bills")
    #
    # numrows = cursor.rowcount

    # for x in range(0, numrows):
    #     row = cursor.fetchone()
    #     q.put(row[0]+"-->"+row[1])
    #
    # db.close()
    # for url in open('urllist.txt'):
    #     q.put(url.strip())

    for x in range(0, 100000):
        q.put('')
    q.join()
except KeyboardInterrupt:
    sys.exit(1)

end_time=datetime.datetime.now()
print(end_time-start_time)

sys.exit(1)
