Esempio n. 1
0
def get_file_from_server(host, port, filename, flag_overwrite=False):
    global _sock
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error as e:
        print("Error creating client socket")
        return
    try:
        client_socket.connect((host, port))
    except socket.error as e:
        client_socket.close()
        print("error connecting to server: %s" % e)
        return
    _sock = client_socket
    signal.signal(signal.SIGURG, urgent_data_handler)
    fcntl.fcntl(client_socket.fileno(), fcntl.F_SETOWN, os.getpid())
    try:
        #get file size
        packed_size = connutils.recv_buffer(client_socket, 8)
        if len(packed_size) != 8:
            return
        #unpack long long format
        server_filesize = struct.unpack("!Q", packed_size)
        if not server_filesize:
            print("Error receiving filesize")
            return
        server_filesize = server_filesize[0]
        print("Server file size %s" % server_filesize)
        try:
            (f, seek_value) = client.create_client_file(filename,
                            server_filesize, flag_overwrite)
        except client.ClientError as e:
            print("Create file error: %s" % e)
            return

        packed_seek = struct.pack("!Q", seek_value)
        if not connutils.send_buffer(client_socket, packed_seek):
            f.close()
            return

        print("Receiving file...")
        bytes_received = fileutils.recv_file(client_socket, f, server_filesize
                - seek_value, progress_callback=recv_progress_handler)
        print("Bytes received %s" % bytes_received)
        if (bytes_received + seek_value) != server_filesize:
            print("!!Not all data received!!")
        f.close()
    except Exception as e:
        print("Client Disconnected: %s" % e)
    finally:
        signal.signal(signal.SIGURG, signal.SIG_DFL)
        client_socket.close()
Esempio n. 2
0
def get_file_from_server(host, port, filename, flag_overwrite=False):
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error as e:
        print("Error creating client socket")
        return
    try:
        client_socket.connect((host, port))
    except socket.error as e:
        client_socket.close()
        print("error connecting to server: %s" % e)
        return
    try:
        #get file size
        packed_size = connutils.recv_buffer(client_socket, 8)
        if len(packed_size) != 8:
            return
        #unpack long long format
        server_filesize = struct.unpack("!Q", packed_size)
        if not server_filesize:
            print("Error receiving filesize")
            return
        server_filesize = server_filesize[0]
        print("Server file size %s" % server_filesize)
        seek_value = 0
        try:
            (f, seek_value) = client.create_client_file(filename,
                            server_filesize, flag_overwrite)
        except client.ClientError as e:
            print("Create file error: %s" % e)
            return
        packed_seek = struct.pack("!Q", seek_value)
        if not connutils.send_buffer(client_socket, packed_seek):
            f.close()
            return

        print("Receiving file...")
        bytes_received = fileutils.recv_file(client_socket, f, server_filesize)
        print("Bytes received %s" % bytes_received)
        if (bytes_received + seek_value) != server_filesize:
            print("!!Not all data received!!")
        f.close()
    except Exception as e:
        print(e)
    finally:
        client_socket.close()
Esempio n. 3
0
 def handle_server_request(self, conn, addr, f):
     """
     Handle single request
     conn - socket connection
     addr - addr info
     f - file object to serve
     """
     #pdb.set_trace()
     print("Client %s:%s - connected" % addr)
     try:
         #send file size first
         file_size = fileutils.get_file_size(f)
         packed_size = struct.pack("!Q", file_size)
         if not connutils.send_buffer(conn, packed_size):
             return
         #recv fileseek
         packed_seek_value = connutils.recv_buffer(conn, 8)
         if len(packed_seek_value) != 8:
             return
         seek_value = struct.unpack("!Q", packed_seek_value)
         if not seek_value:
             return
         seek_value = seek_value[0]
         if seek_value:
             f.seek(seek_value, 0)
             print("%s: Seeking to %s" % (addr, seek_value))
         #send file content
         transfered = fileutils.transfer_file(conn,
                     f, progress_callback=self.send_progress_handler)
         print("%s: Bytes send %d" % (addr, transfered))
         filesize = fileutils.get_file_size(f)
         if transfered != filesize - seek_value:
             print("!! Not all data has been sent !!")
     except socket.error as e:
         print("handle_server_request error %s" % e)
     except KilledThreadException as e:
         print("Thread %s killed with message: %s" % (addr, e))
     finally:
         f.seek(0)
         f.close()
         conn.close()
         print("%s: sended urgent %s" % (addr, self._urg_sended))
         print("Client %s:%s - disconnected" % addr)
Esempio n. 4
0
def handle_server_request(conn, addr, f):
    """
    Handle single request
    conn - socket connection
    addr - addr info
    f - file object to serve
    """
    print("Client %s:%s - connected" % addr)
    try:
        #send file size first
        file_size = fileutils.get_file_size(f)
        packed_size = struct.pack("!Q", file_size)
        if not connutils.send_buffer(conn, packed_size):
            return
        #recv fileseek
        packed_seek_value = connutils.recv_buffer(conn, 8)
        if len(packed_seek_value) != 8:
            return
        seek_value = struct.unpack("!Q", packed_seek_value)
        if not seek_value:
            return
        seek_value = seek_value[0]
        if seek_value:
            f.seek(seek_value, 0)
            print("Seeking to %s" % seek_value)
        #send file content
        transfered = fileutils.transfer_file(conn,
                    f, progress_callback=send_progress_handler)
        print("Bytes send " + str(transfered))
        filesize = fileutils.get_file_size(f)
        if transfered != filesize - seek_value:
            print("!! Not all data has been sent !!")
    except socket.error as e:
        print("handle_server_request error %s" % e)
    finally:
        f.seek(0)
        conn.close()
        print("Client %s:%s - disconnected" % addr)
Esempio n. 5
0
def handle_server_request(server_socket, f):
    """
    f - file object to serve
    """
    client_sockets = []
    #client_info = {
    #"soket" :
    #{"addr":"addr",
    #"seek": 0, "can_write": False, "size_sended": False,"sended":0}
    #}
    client_info = {}
    file_size = fileutils.get_file_size(f)
    packed_size = struct.pack("!Q", file_size)
    buf_size = 1024
    try:
        while True:
            [rfd, wfd, xfd] = select.select([server_socket] + client_sockets,
                client_sockets, client_sockets)
            for r_socket in rfd:
                if server_socket == r_socket:
                    #new connection arrived
                    (conn, addr_info) = server_socket.accept()
                    print("Client %s:%s - connected" % addr_info)
                    client_info[conn] = {
                        "addr": addr_info,
                        "seek": 0,
                        "size_sended": False,
                        "seek_received": False,
                        "sended": 0,
                    }
                    client_sockets.append(conn)
                else:
                    #client socket read
                    if not client_info[r_socket]["size_sended"]:
                        continue
                    if client_info[r_socket]["seek_received"]:
                        continue
                    client_info_record = client_info[r_socket]
                    print("Read request from %s:%s" %
                            client_info_record["addr"])
                    #recv fileseek
                    packed_seek_value = connutils.recv_buffer(r_socket, 8)
                    if len(packed_seek_value) != 8:
                        r_socket.close()
                        client_sockets.remove(r_socket)
                        continue
                    seek_value = struct.unpack("!Q", packed_seek_value)
                    if not seek_value:
                        r_socket.close()
                        client_sockets.remove(r_socket)
                        continue
                    seek_value = seek_value[0]
                    client_info_record["seek"] = seek_value
                    client_info_record["seek_received"] = True
            for w_socket in wfd:
                if not client_info[w_socket]["size_sended"]:
                    if connutils.send_buffer(w_socket, packed_size):
                        client_info[w_socket]["size_sended"] = True
                    else:
                        print("error while send size to %s" %
                            (client_info[w_socket]["addr"]))
                        client_sockets.remove(wsocket)
                        del client_info[wsocket]
                        wsocket.close()
                    continue
                if not client_info[w_socket]["seek_received"]:
                    continue
                #write chunk of data
                client_info_record = client_info[w_socket]
                seek_value = client_info_record["seek"]
                f.seek(seek_value, 0)
                print("%s: seeking to %s" %
                    (client_info_record["addr"], seek_value))
                buffer = f.read(buf_size)
                client_info_record["seek"] = f.tell()
                need_send = len(buffer)
                if not need_send:
                    client_sockets.remove(w_socket)
                    del client_info[w_socket]
                    w_socket.close()
                    print("Client %s disconnected" %
                        (client_info_record["addr"],))
                    continue
                if not connutils.send_buffer(w_socket, buffer):
                    print("Error while send chunk to %s. disconnect" %
                        (client_info_record["addr"],))
                    client_sockets.remove(w_socket)
                    del client_info[w_socket]
                    w_socket.close()
                    continue
                client_info_record["sended"] += need_send
                send_progress_handler(w_socket, client_info_record["sended"])

    finally:
        for client_socket in client_sockets:
            print("Close socket connection %s" %
                [client_info[client_socket]["addr"]])