如何让httpd返回自定义简单字符串?有什么更简便的实现方案?
方案汇总
用busybox httpd实现的方法
完全可以用busybox httpd实现需求,操作步骤如下:
- 首先在当前目录新建
index.html文件,写入你需要返回的自定义字符串即可,不需要额外的HTML标签也可以正常返回。 - 在
index.html所在的目录执行启动命令:busybox httpd -f -p 8000 - 此时访问
localhost:8000即可获得你写在index.html里的自定义内容。如果需要不同路径返回不同内容,对应创建对应的路径目录和文件即可。
更简便的无需创建文件的方案
如果不想额外创建静态文件,可以用以下一行命令直接启动服务:
Python3 环境可用
运行命令,直接替换内容即可:
python3 -c 'from http.server import BaseHTTPRequestHandler; class Handler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200); self.send_header("Content-Type", "text/plain; charset=utf-8"); self.end_headers(); self.wfile.write("你的自定义字符串".encode("utf-8"))' 8000
NetCat(nc) 环境可用
运行命令,直接替换内容即可:
while true; do echo -e "HTTP/1.1 200 OK\nContent-Type: text/plain; charset=utf-8\n\n你的自定义字符串" | nc -l -p 8000; done
每次收到请求都会返回指定内容,按Ctrl+C即可停止服务。
内容的提问来源于stack exchange,提问作者user3877654
相关产品推荐
相关产品推荐

