问题描述
我对python了解不多,需要做一个UDP P2P文件共享应用/代码。以前从未使用过python,所以请原谅我缺乏知识。我需要帮助解决此错误:
`FileNotFoundError: [WinError 2] The system cannot find the file specified`
这是我的 server.py 代码:
import os
import socket
import time
host = input("Host Name: ")
sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)
try:
sock.connect((host,22222))
print("Connected Successfully")
except:
print("Unable to connect")
exit(0)
file_name = sock.recv(100).decode()
file_size = sock.recv(100).decode()
with open("./rec/" + file_name,"wb") as file:
c = 0
start_time = time.time()
while c <= int(file_size):
data = sock.recv(1024)
if not (data):
break
file.write(data)
c += len(data)
end_time = time.time()
print("File transfer Complete. Total time: ",end_time - start_time)
sock.close()
此代码由多个网站和github组成,如有抄袭请见谅。
client.py:
import os
import socket
import time
sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)
sock.bind((socket.gethostname(),22222))
sock.listen(5)
print("Host Name: ",sock.getsockname())
client,addr = sock.accept()
file_name = input("File Name:")
file_size = os.path.getsize(file_name)
client.send(file_name.encode())
client.send(str(file_size).encode())
with open(file_name,"rb") as file:
c = 0
start_time = time.time()
while c <= file_size:
data = file.read(1024)
if not (data):
break
client.sendall(data)
c += len(data)
end_time = time.time()
print("File Transfer Complete. Total time: ",end_time - start_time)
sock.close()
```
解决方法
您遇到的错误消息是由于找不到文件。最有可能是 client.py
文件报告了错误。在尝试打开文件之前,您应该检查该文件是否存在。
file_name = input("File Name: ")
# Ensure the file exists
if not os.path.isfile(file_name):
print(f"File not found: {file_name}")
exit(0)
由于您从不同来源复制和粘贴,代码中有许多错误。下面的代码应该对你有用,我已经添加了注释来解释它是如何工作的。
如果有什么需要进一步解释的,请在评论中告诉我。
server.py:
import os
import socket
import time
SERVER_HOST = "0.0.0.0"
SERVER_PORT = 22222
BUFFER_SIZE = 1024 # Receive 1024 bytes at a time (1kb)
SEPARATOR = ":::::"
SERVER_FOLDER = "./rec/" # Location to save file
# Create the server socket
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
# Bind the socket to the host address and port
sock.bind((SERVER_HOST,SERVER_PORT))
# Enable server to accept connections
sock.listen()
print(f"Server listening {SERVER_HOST}:{SERVER_PORT}")
# Accept client connection
client_socket,address = sock.accept()
print(f"Client {address} connected.")
# Receive the file information
received = client_socket.recv(BUFFER_SIZE).decode()
# Split the string using the separator
file_name,file_size = received.split(SEPARATOR)
print(f"File name: '{file_name}'")
print(f"File size: '{file_size}'")
# Set the path where the file is to be saved
save_path = os.path.join(SERVER_FOLDER,file_name)
with open(save_path,"wb") as file:
bytes_received = 0
start_time = time.time()
while True:
data = client_socket.recv(BUFFER_SIZE)
if not (data):
break
file.write(data)
bytes_received += len(data)
end_time = time.time()
print(f"File transfer Complete. {bytes_received} of {file_size} received. Total time: {end_time - start_time}")
# Close the client socket
client_socket.close()
# Close the server socket
sock.close()
client.py:
import os
import socket
import time
SERVER_PORT = 22222
BUFFER_SIZE = 1024 # Send 1024 bytes at a time (1kb)
SEPARATOR = ":::::"
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
print(f"Connecting to {host}:{SERVER_PORT}")
sock.connect((host,SERVER_PORT))
print("Connected.")
file_name = input("File Name: ")
# Ensure the file exists
if not os.path.isfile(file_name):
print(f"File not found: {file_name}")
exit(0)
# Get the file size
file_size = os.path.getsize(file_name)
# Send file_name and file_size as a single string
sock.send(f"{file_name}{SEPARATOR}{file_size}".encode())
with open(file_name,"rb") as file:
c = 0
start_time = time.time()
while c <= file_size:
data = file.read(BUFFER_SIZE)
if not (data):
break
sock.sendall(data)
c += len(data)
end_time = time.time()
print("File Transfer Complete. Total time: ",end_time - start_time)
sock.close()