1 Star 1 Fork 0

yixuan/pcapcreate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
http_responses.py 1.42 KB
一键复制 编辑 原始数据 按行查看 历史
yixuan 提交于 2024-04-02 13:54 . add file
# -- 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
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/yixuan1/pcapcreate.git
git@gitee.com:yixuan1/pcapcreate.git
yixuan1
pcapcreate
pcapcreate
master

搜索帮助