Python 3.x 客户端如何获得服务器上传的文件名和扩展名的原始文件?

Python 3.x 客户端如何获得服务器上传的文件名和扩展名的原始文件?,python-3.x,sockets,upload,Python 3.x,Sockets,Upload,请帮助我,我只是python的初学者,我想学习这个。我不知道如何从服务器部件获取原始文件名和扩展名 我尝试了很多方法和研究,但仍然不能工作。我看到了许多类型的示例,它们只能上载带有的文本文件,在客户端部分中以open('received_file','.txt','wb')作为f:,并且不能上载文件的多类型扩展名。我知道,因为有'.txt',所以只需处理文本文件即可。我不知道如何声明获得多个扩展名和原始文件名。这是我的原始代码 client import socket import os T

请帮助我,我只是python的初学者,我想学习这个。我不知道如何从服务器部件获取原始文件名和扩展名

我尝试了很多方法和研究,但仍然不能工作。我看到了许多类型的示例,它们只能上载带有
的文本文件,在客户端部分中以open('received_file','.txt','wb')作为f:
,并且不能上载文件的多类型扩展名。我知道,因为有
'.txt'
,所以只需处理文本文件即可。我不知道如何声明获得多个扩展名和原始文件名。这是我的原始代码

client


import socket
import os
TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 8192

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
#data = s.recv(BUFFER_SIZE)


with open('received_file','.txt','wb') as f:
    print ('file opened')
    while True:
        print('receiving data...')
        data = s.recv(BUFFER_SIZE)
        print('data=%s', (data))
        if not data:
            f.close()
            print ('file close()')
            break
        # write data to a file
        f.write(data)

print('Successfully get the file')
s.close()
print('connection closed')
大宗报价


接收到的输出文件名为\u file,不带扩展名

您需要定义一个传输文件名和数据的协议。下面的示例将
input.txt
的内容作为文件名
output.txt
传输到客户端。客户端读取文件名,然后将数据写入该文件名。我之所以这样做,只是因为客户端和服务器运行在同一台计算机上,并且在同一个目录中读取/写入文件

server.py

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        filename = 'output.txt'
        self.request.sendall(filename.encode() + b'\r\n')
        with open('input.txt','rb') as f:
            self.request.sendall(f.read())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9001
    with socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) as server:
        server.serve_forever()
import socket
import os
SERVER = 'localhost',9001

s = socket.socket()
s.connect(SERVER)

# autoclose f and s when with block is exited.
# makefile treats the socket as a file stream.
# Open in binary mode so the bytes of the file are received as is.
with s,s.makefile('rb') as f:
    # The first line is the UTF-8-encoded filename.  Strip the line delimiters.
    filename = f.readline().rstrip(b'\r\n').decode()
    with open(filename,'wb') as out:
        out.write(f.read())
client.py

import socketserver

class MyTCPHandler(socketserver.BaseRequestHandler):
    def handle(self):
        filename = 'output.txt'
        self.request.sendall(filename.encode() + b'\r\n')
        with open('input.txt','rb') as f:
            self.request.sendall(f.read())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9001
    with socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) as server:
        server.serve_forever()
import socket
import os
SERVER = 'localhost',9001

s = socket.socket()
s.connect(SERVER)

# autoclose f and s when with block is exited.
# makefile treats the socket as a file stream.
# Open in binary mode so the bytes of the file are received as is.
with s,s.makefile('rb') as f:
    # The first line is the UTF-8-encoded filename.  Strip the line delimiters.
    filename = f.readline().rstrip(b'\r\n').decode()
    with open(filename,'wb') as out:
        out.write(f.read())

抱歉,为什么我选择的文件将变为空,无法下载以创建新文件?@babolone如果客户端和服务器在同一系统上运行,则输入文件和输出文件不能使用相同的名称;否则,将在打开输出文件时覆盖输入文件。