代码拉取完成,页面将自动刷新
# -- coding: utf-8 --
# Name: http_responses.py
# Where:
def http_response_200():
"""
生成HTTP 200 OK响应。
"""
body = "<html><body><h1>OK</h1></body></html>"
headers = [
"HTTP/1.1 200 OK",
"Content-Type: text/html; charset=UTF-8",
f"Content-Length: {len(body)}",
"Connection: close",
"\r\n"
]
return "\r\n".join(headers) + body
def http_response_302(location='http://example.com'):
"""
生成HTTP 302 Found响应,用于重定向。
"""
headers = [
"HTTP/1.1 302 Found",
f"Location: {location}",
"Content-Length: 0",
"Connection: close",
"\r\n"
]
return "\r\n".join(headers)
def http_response_404():
"""
生成HTTP 404 Not Found响应。
"""
body = "<html><body><h1>Not Found</h1></body></html>"
headers = [
"HTTP/1.1 404 Not Found",
"Content-Type: text/html; charset=UTF-8",
f"Content-Length: {len(body)}",
"Connection: close",
"\r\n"
]
return "\r\n".join(headers) + body
def http_response_502():
"""
生成HTTP 502 Bad Gateway响应。
"""
body = "<html><body><h1>Bad Gateway</h1></body></html>"
headers = [
"HTTP/1.1 502 Bad Gateway",
"Content-Type: text/html; charset=UTF-8",
f"Content-Length: {len(body)}",
"Connection: close",
"\r\n"
]
return "\r\n".join(headers) + body
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。