def getByHTTP(self): """ Implements the HTTP protocol. """ # Start measurement timer. self.timer.startTimer() # Prepare HTTP request if self.length != -1: # If resume requested... # @TODO: Connection: close message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\nRange:bytes=%d-%d\r\n\r\n' % (self.file, self.host, self.port, self.beginResume, self.begin+self.length-1) else: # ...and if not. message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n' % (self.file, self.host, self.port) print message # Send whole message dispatcher.send('DEBUG', 'downloadThread.socket', 'Sending HTTP GET request...') sent = 0 while sent < len(message): sent += self.sock.send(message[sent:]) # Receive response dispatcher.send('DEBUG', 'downloadThread.socket', 'Receiving HTTP response...') msg = '' self.received = self.written # CHANGED!!!! from 0 self.realReceived = 0 self.tempFile.seek(self.written) responseCode = None while 1: data = self.sock.recv(65536) self.received += len(data) if responseCode is None: # If we haven't yet received headers, if msg.find('\r\n\r\n') is not -1: # check if the headers are fully sent. msg += data responseCode, headers = parseHeaders(msg) # If so, parse them. print headers # @DEBUG # Check the response code. # Has to be 206 if used Range:bytes=%d-%d. try: responseCode = int(responseCode) except: dispatcher.send('ERROR', 'downloadThread.HTTP', 'Received weird HTTP code: %s' % (responseCode)) dispatcher.send('STATE_CHANGE', self, (DOWNLOADING_RETRYING,SCRAMBLED_RESPONSE_CODE)) raise BadResponseCode() # if resume support requested if self.length != -1 and responseCode == 200: dispatcher.send('ERROR', 'downloadThread.HTTP', 'No resume support found, but requested(%d).' % (responseCode)) dispatcher.send('STATE_CHANGE', self, (FAILED, NO_RESUME_THOUGH_REQUESTED)) self.__killChildren() raise NoResumeSupportThoughRequested() if (self.length != -1 and responseCode != 206) or (self.length == -1 and responseCode != 200): dispatcher.send('ERROR', 'downloadThread.HTTP', 'Received wrong HTTP code: %d' % (responseCode)) dispatcher.send('STATE_CHANGE', self, (DOWNLOADING_RETRYING,WRONG_RESPONSE_CODE)) raise BadResponseCode() if self.length == -1 and 'Content-Length' in headers: self.length = int(headers['Content-Length']) # Write the bit we received after the headers to the tmp file. self.received -= msg.find('\r\n\r\n')+4 # Don't need to count headers. msg = msg[msg.find('\r\n\r\n')+4:] try: self.tempFile.write(msg) self.written += len(msg) except: print sys.exc_info() dispatcher.send('ERROR', 'downloadThread.tmpfile', 'Failure while writing to local tmp file.') dispatcher.send('STATE_CHANGE', self, (DOWNLOADING_RETRYING,WRITE_TO_TMP_FILE_FAILED)) self.__killChildren() raise WriteToTmpFileFailed() else: msg = msg + data # If the headers haven't been fully received yet. else: self.tempFile.write(data) # Next chunk of downloaded file goes to the tmp file. self.written += len(data) if not data: break # Connection broken, which should mean - end of the file. if self.written == self.length: break # Transfer finished, but not closed by the remote host for some reason or another. if self.kill: # Received a request for the thread to be killed. dispatcher.send('STATE_CHANGE', self, (CANCELLED,)) raise AbortDownloadException() # if self.received != (self.length-self.begin): # dispatcher.send('ERROR', 'downloadThread.HTTP', 'Connection broken, meant to receive: %d, received: %d.' % ((self.length-self.begin), self.received)) # raise IncompleteFileException # Will be raised if connection was closed by the remote site, but transfer has not yet been completed. self.realReceived = self.tempFile.tell() # @DEBUG, I presume self.tempFile.seek(0) dispatcher.send('STATE_CHANGE', self, (FINISHED,)) self.sock.close() self.timer.stopTimer() self.tempFile.flush() print "Received: %d, Written: %d, Should be equal %d.\n" %(self.received, self.written, self.length)
def getByHTTP(self): """ Implements the HTTP protocol. """ # Start measurement timer. self.timer.startTimer() # Prepare HTTP request if self.length != -1: # If resume requested... # @TODO: Connection: close message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\nRange:bytes=%d-%d\r\n\r\n' % ( self.file, self.host, self.port, self.beginResume, self.begin + self.length - 1) else: # ...and if not. message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n' % ( self.file, self.host, self.port) print message # Send whole message dispatcher.send('DEBUG', 'downloadThread.socket', 'Sending HTTP GET request...') sent = 0 while sent < len(message): sent += self.sock.send(message[sent:]) # Receive response dispatcher.send('DEBUG', 'downloadThread.socket', 'Receiving HTTP response...') msg = '' self.received = self.written # CHANGED!!!! from 0 self.realReceived = 0 self.tempFile.seek(self.written) responseCode = None while 1: data = self.sock.recv(65536) self.received += len(data) if responseCode is None: # If we haven't yet received headers, if msg.find( '\r\n\r\n' ) is not -1: # check if the headers are fully sent. msg += data responseCode, headers = parseHeaders( msg) # If so, parse them. print headers # @DEBUG # Check the response code. # Has to be 206 if used Range:bytes=%d-%d. try: responseCode = int(responseCode) except: dispatcher.send( 'ERROR', 'downloadThread.HTTP', 'Received weird HTTP code: %s' % (responseCode)) dispatcher.send( 'STATE_CHANGE', self, (DOWNLOADING_RETRYING, SCRAMBLED_RESPONSE_CODE)) raise BadResponseCode() # if resume support requested if self.length != -1 and responseCode == 200: dispatcher.send( 'ERROR', 'downloadThread.HTTP', 'No resume support found, but requested(%d).' % (responseCode)) dispatcher.send('STATE_CHANGE', self, (FAILED, NO_RESUME_THOUGH_REQUESTED)) self.__killChildren() raise NoResumeSupportThoughRequested() if (self.length != -1 and responseCode != 206) or ( self.length == -1 and responseCode != 200): dispatcher.send( 'ERROR', 'downloadThread.HTTP', 'Received wrong HTTP code: %d' % (responseCode)) dispatcher.send( 'STATE_CHANGE', self, (DOWNLOADING_RETRYING, WRONG_RESPONSE_CODE)) raise BadResponseCode() if self.length == -1 and 'Content-Length' in headers: self.length = int(headers['Content-Length']) # Write the bit we received after the headers to the tmp file. self.received -= msg.find( '\r\n\r\n') + 4 # Don't need to count headers. msg = msg[msg.find('\r\n\r\n') + 4:] try: self.tempFile.write(msg) self.written += len(msg) except: print sys.exc_info() dispatcher.send( 'ERROR', 'downloadThread.tmpfile', 'Failure while writing to local tmp file.') dispatcher.send( 'STATE_CHANGE', self, (DOWNLOADING_RETRYING, WRITE_TO_TMP_FILE_FAILED)) self.__killChildren() raise WriteToTmpFileFailed() else: msg = msg + data # If the headers haven't been fully received yet. else: self.tempFile.write( data ) # Next chunk of downloaded file goes to the tmp file. self.written += len(data) if not data: break # Connection broken, which should mean - end of the file. if self.written == self.length: break # Transfer finished, but not closed by the remote host for some reason or another. if self.kill: # Received a request for the thread to be killed. dispatcher.send('STATE_CHANGE', self, (CANCELLED, )) raise AbortDownloadException() # if self.received != (self.length-self.begin): # dispatcher.send('ERROR', 'downloadThread.HTTP', 'Connection broken, meant to receive: %d, received: %d.' % ((self.length-self.begin), self.received)) # raise IncompleteFileException # Will be raised if connection was closed by the remote site, but transfer has not yet been completed. self.realReceived = self.tempFile.tell() # @DEBUG, I presume self.tempFile.seek(0) dispatcher.send('STATE_CHANGE', self, (FINISHED, )) self.sock.close() self.timer.stopTimer() self.tempFile.flush() print "Received: %d, Written: %d, Should be equal %d.\n" % ( self.received, self.written, self.length)
def checkHTTP(self, sock, port): host = self.url[self.url.find('//') + 2:] self.file = host[host.find('/'):] host = host[:host.find('/')] message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\nRange:bytes=%d-%d\r\n\r\n' % ( self.file, host, port, 0, 1024) try: sock.connect((host, port)) except: dispatcher.send('ERROR', 'TaskClass.chceckHttpServer', 'Unable to connect, check url?') raise BadConnect('Can\'t connect to %s' % host) sent = 0 while sent < len(message): sent += sock.send(message[sent:]) httpHeader = '' while True: data = sock.recv(512) httpHeader = httpHeader + data if httpHeader.find('\r\n\r\n') is not -1: break code, headers = http.parseHeaders(httpHeader) if int(code) != 206: # clue of this too long method self.supportsResume = False dispatcher.send('DEBUG', 'TaskClass', 'Server not supporting resume') if int(code) == 404 or int(code) == 302: dispatcher.send('ERROR', 'TaskClass', 'Error 404') raise BadConnect('404') sock.close() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((host, port)) except: dispatcher.send('ERROR', 'TaskClass.chceckHttpServer', 'Unable to connect, check url?') raise BadConnect('Can\'t connect to %s' % host) # TODO, might be more elegant maybe? message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n' % (self.file, host, port) sent = 0 while sent < len(message): sent += sock.send(message[sent:]) httpHeader = '' while True: data = sock.recv(512) httpHeader = httpHeader + data if httpHeader.find('\r\n\r\n') is not -1: break code, headers = http.parseHeaders(httpHeader) print code, headers self.size = int(headers['content-length']) sock.close()
def checkHTTP(self, sock, port): host = self.url[self.url.find('//') + 2 : ] self.file = host[host.find('/') : ] host = host[ : host.find('/')] message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\nRange:bytes=%d-%d\r\n\r\n' % (self.file, host, port, 0, 1024) try: sock.connect((host, port)) except: dispatcher.send('ERROR', 'TaskClass.chceckHttpServer', 'Unable to connect, check url?') raise BadConnect('Can\'t connect to %s' % host) sent = 0 while sent < len(message): sent += sock.send(message[sent:]) httpHeader = '' while True: data = sock.recv(512) httpHeader = httpHeader + data if httpHeader.find('\r\n\r\n') is not -1: break code, headers = http.parseHeaders(httpHeader) if int(code) != 206: # clue of this too long method self.supportsResume = False dispatcher.send('DEBUG', 'TaskClass', 'Server not supporting resume') if int(code) == 404 or int(code) == 302: dispatcher.send('ERROR', 'TaskClass', 'Error 404') raise BadConnect('404') sock.close() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect((host, port)) except: dispatcher.send('ERROR', 'TaskClass.chceckHttpServer', 'Unable to connect, check url?') raise BadConnect('Can\'t connect to %s' % host) # TODO, might be more elegant maybe? message = u'GET %s HTTP/1.1\r\nHost: %s:%d\r\n\r\n' % (self.file, host, port) sent = 0 while sent < len(message): sent += sock.send(message[sent:]) httpHeader = '' while True: data = sock.recv(512) httpHeader = httpHeader + data if httpHeader.find('\r\n\r\n') is not -1: break code, headers = http.parseHeaders(httpHeader) print code,headers self.size = int(headers['content-length']) sock.close()