[英]WinError2 File Specified Not Found (Python 3.9)
我对 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 组成,如果您看到任何复制,请见谅。
客户端.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)
由于您从不同来源复制和粘贴,代码中存在许多错误。 下面的代码应该适合你,我添加了注释来解释它是如何工作的。
如果有什么需要进一步解释的,请在评论中告诉我。
服务器.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()
客户端.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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.