为什么我得到[Errno 2]?
因此,我创建了一台服务器,因为目前执行一个非常简单的任务,它假设将数据包发送到浏览器中包含“ datatatata”字符串。 但是它在运行它并从浏览器发送请求时不起作用,我会得到[ERRNO 2]。该请求当然是HTTP请求 代码:
import socket
# functions
def get_file_data(filename):
pass
def handle_client_request(file_path, client_socket):
file_to_return = open(file_path, 'rb')
file_content = file_to_return.read()
client_socket.send(file_content)
def validate_client_request(client_request):
# checks validation of request. for now validation is set to true
validation = True
# checks for requested URL
desired_file_name = ""
for i in client_request[5::]:
if i != " ":
desired_file_name += i
else:
break
# checking file type
file_type = ""
if desired_file_name == "favicon":
file_type = "ico"
elif desired_file_name == "abstruct" or desired_file_name == "":
file_type = "jpg"
elif desired_file_name == "loading":
file_type = "gif"
if file_type == "":
validation = False
file_path = "C:\\Users\\hadad\\Desktop\\webroot\\imgs\\" + desired_file_name + "." + file_type
return validation, file_path
def handle_client(client_socket):
client_request = client_socket.recv(1024).decode()
valid_request, file_path = validate_client_request(client_request)
if valid_request:
handle_client_request(file_path, client_socket)
else:
client_socket.send("unvalid request. connection ended".encode())
client_socket.close()
def main():
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 80))
server_socket.listen()
# everlasting loop
while True:
client_socket, client_address = server_socket.accept()
handle_client(client_socket)
if __name__ == '__main__':
main()
错误:
Traceback (most recent call last):
File "C:/Networks/hed/good_server.py", line 64, in <module>
main()
File "C:/Networks/hed/good_server.py", line 60, in main
handle_client(client_socket)
File "C:/Networks/hed/good_server.py", line 46, in handle_client
handle_client_request(file_path, client_socket)
File "C:/Networks/hed/good_server.py", line 11, in handle_client_request
file_to_return = open(file_path, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hadad\\Desktop\\webroot\\imgs\\abstruct.jpg'
so I have created a server that as for now does a very simple task, it suppose to send to the browser a packet whitch contains the string "datatatata".
but it does'nt work, when running it and sending a request from browser, I keep getting [Errno 2]. the request is of course HTTP request
the code:
import socket
# functions
def get_file_data(filename):
pass
def handle_client_request(file_path, client_socket):
file_to_return = open(file_path, 'rb')
file_content = file_to_return.read()
client_socket.send(file_content)
def validate_client_request(client_request):
# checks validation of request. for now validation is set to true
validation = True
# checks for requested URL
desired_file_name = ""
for i in client_request[5::]:
if i != " ":
desired_file_name += i
else:
break
# checking file type
file_type = ""
if desired_file_name == "favicon":
file_type = "ico"
elif desired_file_name == "abstruct" or desired_file_name == "":
file_type = "jpg"
elif desired_file_name == "loading":
file_type = "gif"
if file_type == "":
validation = False
file_path = "C:\\Users\\hadad\\Desktop\\webroot\\imgs\\" + desired_file_name + "." + file_type
return validation, file_path
def handle_client(client_socket):
client_request = client_socket.recv(1024).decode()
valid_request, file_path = validate_client_request(client_request)
if valid_request:
handle_client_request(file_path, client_socket)
else:
client_socket.send("unvalid request. connection ended".encode())
client_socket.close()
def main():
server_socket = socket.socket()
server_socket.bind(("0.0.0.0", 80))
server_socket.listen()
# everlasting loop
while True:
client_socket, client_address = server_socket.accept()
handle_client(client_socket)
if __name__ == '__main__':
main()
the error:
Traceback (most recent call last):
File "C:/Networks/hed/good_server.py", line 64, in <module>
main()
File "C:/Networks/hed/good_server.py", line 60, in main
handle_client(client_socket)
File "C:/Networks/hed/good_server.py", line 46, in handle_client
handle_client_request(file_path, client_socket)
File "C:/Networks/hed/good_server.py", line 11, in handle_client_request
file_to_return = open(file_path, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hadad\\Desktop\\webroot\\imgs\\abstruct.jpg'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论