服务器未接收Python中从客户端发送的所有文件

服务器未接收Python中从客户端发送的所有文件,python,Python,我已经用Python编写了客户机和服务器代码。在代码中,客户机从用户处获取一条路径作为输入。路径可以是文件夹或文件的路径。之后,客户端将遍历该文件夹/目录,并将其中及其子文件夹中的所有文件发送到服务器,但问题是,尽管客户端将所有文件发送到服务器,但服务器只接收到一个文件 有什么问题 客户端代码: 服务器代码: 所以,这里的问题是您只创建了第一个文件一次。然后将所有接收到的内容文件路径也写入同一个文件 else: while True: path = connectio

我已经用Python编写了客户机和服务器代码。在代码中,客户机从用户处获取一条路径作为输入。路径可以是文件夹或文件的路径。之后,客户端将遍历该文件夹/目录,并将其中及其子文件夹中的所有文件发送到服务器,但问题是,尽管客户端将所有文件发送到服务器,但服务器只接收到一个文件

有什么问题

客户端代码:

服务器代码:


所以,这里的问题是您只创建了第一个文件一次。然后将所有接收到的内容文件路径也写入同一个文件

else:  
    while True:
        path = connectionSocket.recv(1024)
        print(path)
        if path.decode('utf-8')=="":
            break
        fileName = os.path.basename(path).decode('utf-8')
        drive, tail = os.path.splitdrive( os.path.dirname(path) )
        directory = os.getcwd()+tail.decode('utf-8')
        if not os.path.exists(directory):
            **os.makedirs(directory)**
在下面的while循环中,您只是将数据写入上面创建的文件

    while (data):
        file.write(data)
        data = connectionSocket.recv(1024)
    file.close()

您需要为上述while循环中的每次读取添加创建新文件。

您可以使用此客户端和服务器以同步模式传输文件。传输是一个文件一个文件地进行的,当客户端向服务器发送数据时,它希望服务器做出响应,通过这种方式,我创建了一些传输文件的协议。您可能需要根据需要修改某些语句

我只修改了多个文件的情况下,请记住这一点

客户:

服务器:


注意:这是我尝试过的一个示例代码,您可以改进它。

我每次打开新文件file=open directory+\\\+fileName,在第36行“wb”是的,但按照您的逻辑,它只在第一次迭代中创建文件,剩余的所有迭代只会向其添加数据。@RaeesKhan添加更多数据,您的逻辑完全是异步的。客户端从不等待服务器,服务器也从不等待客户端。您需要设置一些协议来逐个文件或通过线程处理多个文件来处理此操作。
else:  
    while True:
        path = connectionSocket.recv(1024)
        print(path)
        if path.decode('utf-8')=="":
            break
        fileName = os.path.basename(path).decode('utf-8')
        drive, tail = os.path.splitdrive( os.path.dirname(path) )
        directory = os.getcwd()+tail.decode('utf-8')
        if not os.path.exists(directory):
            **os.makedirs(directory)**
    while (data):
        file.write(data)
        data = connectionSocket.recv(1024)
    file.close()
from socket import *
import fnmatch
import os


serverIP = '127.0.0.1'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverIP,serverPort))
path = raw_input('Input Path:')


if os.path.exists(path):
    clientSocket.send(bytes(path))
    path_resp = clientSocket.recv(1024)


    if os.path.isfile(path):
        file = open(path, "rb")
        data = file.read(1024)
        while (data):
            clientSocket.send(data)
            data = file.read(1024)
        file.close()


    else:
        file_list = []
        for root, dirnames, filenames in os.walk(path):
            for filename in fnmatch.filter(filenames, '*.*'):
                file_list.append(os.path.join(root, filename).encode('utf-8'))
        print file_list
        file_list = iter(file_list)
        while True:
            file_transfer_done = False
            try:
                file_name = file_list.next()
            except StopIteration, e:
                str(e)
                break

            print('Sent file name to server %s' % file_name)
            clientSocket.send(file_name)
            file_resp = clientSocket.recv(1024)
            print('Received server response : %s' % file_resp)
            file = open(file_name, "rb")
            data = file.read(1024)
            while True:
                if data:
                    clientSocket.send(data)
                    print('Sent data to server')
                    data_resp = clientSocket.recv(1024)
                    print('Received server responce : %s' % data_resp)
                data = file.read(1024)
                if not data:
                    clientSocket.send('file_closed')
                    close_resp = clientSocket.recv(1024)
                    break
            file.close()
        clientSocket.send("close".encode('utf-8'))


else:
    print("Path is invalid!")

clientSocket.close()
print("connection closed")
from socket import *
import os


serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(1)
print('The server is ready to receive')
connectionSocket, address = serverSocket.accept()
path = connectionSocket.recv(1024)

connectionSocket.send('path_received')

if os.path.isfile(path):
    file = open(os.path.basename(path), 'wb' )
    data = connectionSocket.recv(1024)
    while (data):
        file.write(data)
        data = connectionSocket.recv(1024)
    file.close()


else:
    while True:
        path = connectionSocket.recv(1024)
        print('Received file from client: %s' % path)
        connectionSocket.send('file_path_received')
        print('Sent responce to client : %s' % ('file_path_received'))

        if path.decode('utf-8')=="":
            break
        fileName = os.path.basename(path)
        print('File name received : %s' % fileName)
        drive, tail = os.path.splitdrive( os.path.dirname(path) )
        directory = os.getcwd()+tail
        print directory
        if not os.path.exists(directory):
            os.makedirs(directory)

        file = open( directory +"/"+ fileName, 'wb' )
        print('New file created : %s' % fileName)
        data = connectionSocket.recv(1024)

        while True:
            file.write(data)
            print('File write done')
            connectionSocket.send('Processed')
            data = connectionSocket.recv(1024)
            if 'file_closed' in data:
                connectionSocket.send('file_done')
                break
        file.close()


connectionSocket.close()
print("connection closed")