提问人:Mertcan Bilek 提问时间:11/18/2023 最后编辑:Remy LebeauMertcan Bilek 更新时间:11/18/2023 访问量:43
Python Socket Raw HTTP 服务器发送图像问题
Python Socket Raw HTTP server sending image problem
问:
我尝试发送像HTML和CSS文档一样的图像。在开发人员控制台上,它似乎是成功的,但是当我想在我的网页上看到它时,它似乎不起作用。它们未正确发送。
第一张图片:
第二张图片:
开发者控制台:
实际的第一张图片:
def handle_response(request_data):
method, path, protocol = request_data.split("\r\n")[0].split(" ")
print(method)
print(path)
print(protocol)
if path == "/":
file_path = "website" + path + "index.html"
else:
file_path = "website" + path
print(file_path)
if os.path.exists(file_path):
with open(file_path, "rb") as response_file:
response_content = response_file.read()
response_type, _= mimetypes.guess_type(file_path)
if "text" in response_type:
response = f"HTTP/1.1 200 OK\r\nContent-Type: {response_type}\r\n\r\n{response_content.decode('utf-8')}"
elif "image" in response_type:
print(response_type)
response = f"HTTP/1.1 200 OK\r\nContent-Type: {response_type}\r\n\r\n{response_content}"
else:
response = "HTTP/1.1 404 Not Found\r\n\r\n<h1>404 Not Found</h1>"
return response
答: 暂无答案
评论
print
Content-Length