找不到指定的WinError2文件(Python 3.9)
我对python了解不多,我需要制作一个UDP P2P文件共享应用程序/代码。以前从未使用过python,所以请原谅我缺乏知识。我需要获得此错误的帮助:找不到指定的WinError2文件(Python 3.9),python,file,sharing,Python,File,Sharing,我对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: &quo
`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()
导入操作系统
导入套接字
导入时间
主机=输入(“主机名:”)
sock=socket.socket(socket.AF\u INET,socket.sock\u流)
尝试:
sock.connect((主机,22222))
打印(“连接成功”)
除:
打印(“无法连接”)
出口(0)
文件名=sock.recv(100).decode()
文件大小=sock.recv(100).decode()
打开(“./rec/”+文件名,“wb”)作为文件:
c=0
开始时间=time.time()
而您遇到的错误消息是由于找不到文件。很可能是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()
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()
客户端.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()
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()
导入操作系统
导入套接字
导入时间
服务器端口=22222
缓冲区大小=1024#一次发送1024字节(1kb)
分隔符=“::::”
sock=socket.socket(socket.AF\u INET,socket.sock\u流)
host=socket.gethostname()
打印(f“连接到{host}:{SERVER\u PORT}”)
sock.connect((主机、服务器\端口))
打印(“已连接”)
文件名=输入(“文件名:”)
#确保该文件存在
如果不是os.path.isfile(文件名):
打印(f“未找到文件:{File_name}”)
出口(0)
#获取文件大小
file\u size=os.path.getsize(文件名)
#将文件名和文件大小作为单个字符串发送
send(f“{file\u name}{SEPARATOR}{file\u size}”.encode())
打开(文件名为“rb”)作为文件:
c=0
开始时间=time.time()
而c你已经为server.py
和client.py
发布了相同的代码,哈哈,我的错。谢谢,已修复。请编辑您的问题以包含异常的完整回溯。这会告诉你问题出在哪里。根据你的代码。我应该如何运行它。我试着输入了我的文件名,但我不明白“/rec/”是什么意思。它有什么作用?获取未找到“/rec/”目录的错误。再说一遍,我对python一无所知。谢谢。“./rec/”将是服务器文件所在位置中名为“rec”的目录。我直接从你在问题中提供的代码中获取。