Python客户端服务端文件传输代码:目录与登录验证问题求助
文件传输代码问题排查与修复建议
核心问题
- Windows系统下Python无法识别指定目录,运行时提示目录不存在;
- 基于字典的登录验证系统失效,无论输入何种账号密码均能登录;
- 文件传输的读写逻辑存在错误,导致功能无法正常运行。
客户端代码
import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. x = True username = input('Username: ') password = input('Password: ') Login = { "joshua": "macOS", #regardless of information used to login, still logs you in "scotto": "linux", "divetta": "windows", } print("testing1") #prints but crashes too quickly print("Successfully Logged In") s.connect((host, port)) choice = int(input("1=Upload or 2=Download: ")) while x == True: if choice == 1: print("upload") upload = input("File name to upload: ") f = open('Client\\{upload}','rb') #ask about how to get folder to work here with filename print('Sending...') l = f.read(1024) #ask if this will make the file transfer, transfer data while (l): print('Sending...') s.send(l) l = f.read(1024) x = False f.close() print("Done Sending") s.shutdown(socket.SHUT_WR) print(s.recv(1024)) s.close() elif choice == 2: print("download") download = input("File name to download: ") f = open('Client\\{download}','rb') #ask about how to get folder to work here with filename print('Receiving...') l = f.write(1024) while (l): print('Receiving...') s.recv(l) l = f.write(1024) x = False f.close() print("Done Receiving") s.shutdown(socket.SHUT_WR) print(s.recv(1024)) s.close() else: print("invalid choice") x = True
服务端代码
import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 12345 # Reserve a port for your service. s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. c, addr = s.accept() # Establish connection with client. print('Got connection from', addr) Login = { "joshua": "macOS", "scotto": "linux", "divetta": "windows", } while True: l = c.recv(1024) while (l): print("Receiving...") f.write(l) l = c.recv(1024) f.close() print("Done Receiving") #c.close() # Close the connection while False: l = c.send(1024) while (l): print("Sending...") f.read(l) l = c.send(1024) f.close() print("Done Sending") #c.close()
问题修复方案
1. 登录验证失效修复
当前代码未对账号密码做任何校验,直接跳过验证步骤,需添加以下逻辑:
# 替换原有的print("Successfully Logged In")代码块 if Login.get(username) == password: print("Successfully Logged In") s.connect((host, port)) # 后续上传/下载逻辑放在这个分支内 else: print("Invalid username or password") exit()
同时服务端也需要同步验证逻辑:客户端发送账号密码后,服务端校验并返回结果,避免无效连接占用资源。
2. Windows目录识别问题修复
- 字符串格式化错误:
'Client\\{upload}'未正确替换变量,需改为f-string格式:f'Client\\{upload}' - 路径兼容性:建议使用
os.path模块拼接路径,避免Windows下的转义问题:
import os # 替换打开文件的代码 file_path = os.path.join("Client", upload) f = open(file_path, 'rb')
- 文件夹存在性:确保
Client文件夹在脚本运行的当前目录下,或使用绝对路径指定位置。
3. 文件传输读写逻辑修复
客户端上传逻辑
- 移除循环内的
x = False,否则仅发送一次数据就终止循环:
l = f.read(1024) while l: print('Sending...') s.send(l) l = f.read(1024) x = False # 发送完成后再终止外层循环
客户端下载逻辑
- 下载时文件需用
'wb'(二进制写入)模式打开,而非'rb';f.write()需接收socket返回的字节数据,而非传入数字:
download = input("File name to download: ") file_path = os.path.join("Client", download) f = open(file_path, 'wb') print('Receiving...') l = s.recv(1024) while l: print('Receiving...') f.write(l) l = s.recv(1024) f.close()
服务端逻辑
- 未定义文件对象
f就调用f.write(),需先接收文件名再创建文件:
# 服务端处理上传示例 while True: filename = c.recv(1024).decode() if not filename: break file_path = os.path.join("Server", filename) with open(file_path, 'wb') as f: while True: data = c.recv(1024) if not data: break f.write(data) print("Done Receiving") c.send(b"File received successfully")
while False:块永远不会执行,下载逻辑需重新编写:先接收客户端的下载请求,再读取对应文件并发送。
内容的提问来源于stack exchange,提问作者Joshua Scotto Divetta
相关产品推荐
相关产品推荐

