예제 #1
0
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
예제 #2
0
 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, ))
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
 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)
예제 #6
0
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)
예제 #7
0
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
예제 #8
0
 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
예제 #9
0
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)
예제 #10
0
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