我需要在不使用HTTP库的情况下构建http服务器。
我正在运行服务器并加载了一个html页面,但我的<img src="..."/>
标签没有加载,我收到了通话,但无法在页面中预设png / JPEG。
# Define socket host and port
SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8000
# Create socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((SERVER_HOST, SERVER_PORT))
server_socket.listen(1)
print('Listening on port %s ...' % SERVER_PORT)
while True:
# Wait for client connections
client_connection, client_address = server_socket.accept()
# Handle client request
request = client_connection.recv(1024).decode()
content = handle_request(request)
# Send HTTP response
if content:
response = 'HTTP/1.1 200 OK\n\n'
response += content
else:
response = 'HTTP/1.1 404 NOT FOUND\n\nFile Not Found'
client_connection.sendall(response.encode())
client_connection.close()
# Close socket
server_socket.close()
def handle_request(request):
http = HttpHandler.HTTPHandler
# Parse headers
print(request)
headers = request.split('\n')
get_content = headers[0].split()
accept = headers[6].split()
type_content = accept[1].split('/')
try:
# Filename
filename = get_content[1]
if get_content[0] == "GET":
content = http.get(None, get_content[1], type_content[0])
return content
except FileNotFoundError:
return None
class HTTPHandler:
def get(self, args, type):
if args == '/':
args = '/index.html'
fin = open('htdocs' + args)
if type != "image":
fin = open('htdocs/' + args)
if type == "image":
fin = open('htdocs/' + args, 'rb')
# Read file contents
content = fin.read()
fin.close()
return content
意识到我正在尝试创建一个HTTP 1.1,如果你看到任何模式之外可以提前表示感谢。
答案 0 :(得分:3)
我不知道你在哪里学习了HTTP的工作方式,但我很确定你没有学习the actual standard在实现协议时你应该做什么。关于您的实施的一些注意事项:
\r\n
而不是\n
。对于来自客户端的请求,来自服务器的响应都是如此。recv
内读取。但是,请求可以具有任意长度,并且不能保证您在单个recv
内获得所有请求(TCP是流协议而不是消息协议)。Content-length
标题中包含正文的长度或使用分块传输编码。Content-Type
标题提供内容类型,即HTML为Content-type: text/html
,JPEG图像为Content-type: image/jpeg
。如果没有这个浏览器可能正确或错误地猜测类型可能是什么,或者根据上下文可能也会坚持使用正确的内容类型标题。除此之外,如果您调试此类问题,找出客户端和服务器之间实际交换的内容会很有帮助。可能是您已经为自己检查了这个,但是您没有将这些信息包含在您的问题中。您唯一的错误说明是“...我接听了电话,但无法在页面”中预设png / JPEG,然后转储您的代码。
答案 1 :(得分:1)
结束如:
while True:
# Wait for client connections
client_connection, client_address = server_socket.accept()
# Handle client request
request = client_connection.recv(10240).decode()
content = handle_request(request)
# Send HTTP response
if content:
if str(content).find("html") > 0:
client_connection.send('HTTP/1.1 200 OK\n\n'.encode())
client_connection.send(content.encode())
else:
client_connection.send('HTTP/1.1 200 OK\r\n'.encode())
client_connection.send("Content-Type: image/jpeg\r\n".encode())
client_connection.send("Accept-Ranges: bytes\r\n\r\n".encode())
client_connection.send(content)
else:
response = 'HTTP/1.1 404 NOT FOUND\r\nFile Not Found'
client_connection.close()
Get方法如:
class HTTPHandler:
def get(self, args, type):
if args == '/':
args = '/index.html'
fin = open('htdocs' + args)
if type != "image":
fin = open('htdocs/' + args)
if type.find("html") == -1:
image_data = open('htdocs/' + args, 'rb')
bytes = image_data.read()
# Content-Type: image/jpeg, image/png \n\n
content = bytes
fin.close()
return content
# Read file contents
content = fin.read()
fin.close()
return content