rendered paste bodyclass HttpDownload(threading.Thread): def __init__(self, url, output_file = None, start_byte = None): threading.Thread.__init__(self) self.m_stop = False self.m_data = "" self.m_TotallyWritten = 0 self.m_MimeLock = threading.Lock() self.m_DataLock = threading.Lock() self.m_TotalWrittenLock = threading.Lock() # constructor parameters self.m_url = url self.m_outputfile = output_file self.m_start_byte = start_byte def run(self): parts = urlparse.urlsplit(self.m_url) host_port = parts[1].split(":") host = host_port[0] global port if len(host_port) == 1: port = 80 else: port = int(host_port[1]) self.m_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: self.m_socket.connect((host, port)) except: print "Unable to connect to " + host + " at port " + port self.m_socket.close() return False request = self.Request(host, parts[2], self.m_start_byte) print request self.m_socket.send(request) if not self.GetDownload(self.m_outputfile, self.m_start_byte): print "Error downloading " + self.m_url def Request(self, domain, path, start_byte = None): request =\ "GET " + path + " HTTP/1.1\n" +\ "Host: " + domain if start_byte is not None: request += "\n" + \ "Range: bytes=" + str(start_byte) + "-\n\n" else: request += "\n\n" return request def GetDownload(self, outputfile, start_byte = None): fo = None if outputfile: print "Bestand geopent" if start_byte: fo = open(outputfile, "ab") else: fo = open(outputfile, "wb") bigbuffer = "" notfound = True loc = -1 while notfound: tmpbuf = self.m_socket.recv(1024) bigbuffer += tmpbuf loc = bigbuffer.find("\r\n\r\n") if loc > -1: notfound = False header = bigbuffer[0:loc] # We lock the function call because we don't want any other function to start reading # self.m_mime before it's done. self.m_MimeLock.acquire() error_code = self.ParseHeader(header) self.m_MimeLock.release() if error_code[0] is not "2": print "PANIC!!! Error-code nog 2xx" return False bigbuffer = bigbuffer[loc+4:] if self.m_header.has_key("transfer-encoding") and self.m_header["transfer-encoding"] == "chunked": pass else: message_length = int(self.m_header["content-length"]) while not self.m_stop: datachunk = self.m_socket.recv(1024) if fo: fo.write(datachunk) else: self.m_DataLock.acquire() self.m_data += datachunk self.m_DataLock.release() self.m_TotalWrittenLock.acquire() self.m_TotallyWritten += len(datachunk) print float(self.m_TotallyWritten) / float(message_length) * 100.0 if self.m_TotallyWritten >= message_length or datachunk == "": self.m_stop = True self.m_TotalWrittenLock.release() self.m_socket.close() if fo: fo.close() return True def ParseHeader(self, header): lines = header.splitlines() self.m_ResponseStatus = lines[0].split()[1] self.m_header = { } lines = lines[1:] for line in lines: line = line.lower() print line colon = line.find(":") self.m_header[line[:colon].strip()] = line[colon+1:].strip() return self.m_ResponseStatus