使用TCP - Server端进行文件传输

时间:2014-10-06 23:40:14

标签: python

我正在尝试将文件从服务器发送到客户端,但服务器没有发送文件。有人可以帮助我吗?

while 1:

    # server waits for incoming requests; new socket created on return
    connection_client_socket, addr = connection_server_socket.accept()

    # Read the name of the file sent from the client
    file_name_client = connection_client_socket.recv(1024)

    file_name = file_name_client.decode('utf-8')

我希望这个程序可以正常工作直到这条线。

    file_name_to_look_for = file_name
    file_search_status = 0
    for root, dirs, files in os.walk('C:\\Users\\welcome\\Desktop\\Arun\\'):
        if file_name_to_look_for in files:
            file_search_status = 1
            print("File found at : %s" % join(root, file_name_to_look_for))
            break
    if file_search_status == 0:
        ## 
        #   The file requested by the client is not found on the server
        ##
        capitalized_file_name_client = "FILE NOT FOUND".encode()
        connection_client_socket.send(capitalized_file_name_client)
    else:
        #   The file has been found on the server
        #   send notification to client stating that the file transfer begins
        capitalized_file_name_client = "FILE FOUND".encode()
        connection_client_socket.send(capitalized_file_name_client)
        begin_file_transfer(file_name,connection_client_socket)

# close the TCP connection
# welcoming socket continues
connection_client_socket.close()

0 个答案:

没有答案