def tcp_recv_file(conn, local_filename, file_size, buffer_size=1024): ''' Receive file from server, fragmented it while receiving and write to disk. arguments: @conn: connection @local_filename: string @file_size: int, remote file size @buffer_size: int, receive buffer size @Return int: file size if success else raise ConnectionError. ''' total_file_size = 0 flush_size = 0 remain_bytes = file_size with open(local_filename, 'wb+') as f: while remain_bytes > 0: try: if remain_bytes >= buffer_size: file_buffer, recv_size = tcp_recv_response(conn, buffer_size, buffer_size) else: file_buffer, recv_size = tcp_recv_response(conn, remain_bytes, buffer_size) f.write(file_buffer) remain_bytes -= buffer_size total_file_size += recv_size flush_size += recv_size if flush_size >= 4096: f.flush() flush_size = 0 except ConnectionError as e: raise ConnectionError('[-] Error: while downloading file(%s).' % e.args) except IOError as e: raise DataError('[-] Error: while writting local file(%s).' % e.args) return total_file_size
def send_header(self, conn): '''Send Tracker header to server.''' header = self._pack(self.pkg_len, self.cmd, self.status) try: conn._sock.sendall(header) except (socket.error, socket.timeout) as e: raise ConnectionError('[-] Error: while writting to socket: %s' % (e.args, ))
def disconnect(self): '''Disconnect from fdfs server.''' if self._sock is None: return try: self._sock.close() except socket.error as e: raise ConnectionError(self._errormessage(e)) self._sock = None
def connect(self): '''Connect to fdfs server.''' if self._sock: return try: sock = self._connect() except socket.error as e: raise ConnectionError(self._errormessage(e)) self._sock = sock
def recv_header(self, conn): '''Receive response from server. if sucess, class member (pkg_len, cmd, status) is response. ''' try: header = conn._sock.recv(self.header_len()) except (socket.error, socket.timeout) as e: raise ConnectionError('[-] Error: while reading from socket: %s' % (e.args, )) self._unpack(header)
def tcp_send_data(conn, bytes_stream): '''Send buffer to server. It is not include tracker header. arguments: @conn: connection @bytes_stream: trasmit buffer @Return bool ''' try: conn._sock.sendall(bytes_stream) except (socket.error, socket.timeout) as e: raise ConnectionError('[-] Error: while writting to socket: (%s)' % e.args)
def tcp_send_file_ex(conn, file, buffer_size=8*1024): file_size = 0 file.seek(0) while 1: try: send_buffer = file.read(buffer_size) send_size = len(send_buffer) if send_size == 0: file.close() break tcp_send_data(conn, send_buffer) file_size += send_size except ConnectionError as e: raise ConnectionError('[-] Error while uploading file(%s).' % e.args) except IOError as e: raise DataError('[-] Error while reading local file(%s).' % e.args) return file_size
def make_conn(self): '''Create a new connection.''' if self._conns_created >= self.max_conn: raise ConnectionError('[-] Error: Too many connections.') num_try = 10 while True: try: if num_try <= 0: sys.exit() conn_instance = self.conn_class(**self.conn_kwargs) conn_instance.connect() self._conns_created += 1 break except ConnectionError as e: print(e) num_try -= 1 conn_instance = None return conn_instance
def tcp_recv_response(conn, bytes_size, buffer_size=4096): '''Receive response from server. It is not include tracker header. arguments: @conn: connection @bytes_size: int, will be received byte_stream size @buffer_size: int, receive buffer size @Return: tuple,(response, received_size) ''' recv_buff = [] total_size = 0 try: while bytes_size > 0: resp = conn._sock.recv(buffer_size) recv_buff.append(resp) total_size += len(resp) bytes_size -= len(resp) except (socket.error, socket.timeout) as e: raise ConnectionError('[-] Error: while reading from socket: (%s)' % e.args) return (b''.join(recv_buff), total_size)
def tcp_send_file(conn, filename, buffer_size=8*1024): ''' Send file to server, and split into multiple pkgs while sending. arguments: @conn: connection @filename: string @buffer_size: int ,send buffer size @Return int: file size if success else raise ConnectionError. ''' file_size = 0 with open(filename, 'rb') as f: while 1: try: send_buffer = f.read(buffer_size) send_size = len(send_buffer) if send_size == 0: break tcp_send_data(conn, send_buffer) file_size += send_size except ConnectionError as e: raise ConnectionError('[-] Error while uploading file(%s).' % e.args) except IOError as e: raise DataError('[-] Error while reading local file(%s).' % e.args) return file_size