首页 > 解决方案 > Python http.server .path 返回 / 而不是 protocol://netloc/path#fragment

问题描述

Python 3.8 正在使用一个简单的 http 服务器,并期望 self.path 给我整个 url,因为我可以找到每个 urllib.parse 示例,将 self.path 分解成各自的部分,但是,我得到的只是'/'。

TBH 我意识到我可能搞错了,但我已经到了无处可去的阶段。

这是一个简化的服务器,我希望输出类似“http://localhost:8000#test”但我得到的只是“/”

from http.server import HTTPServer, BaseHTTPRequestHandler

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        outstr=self.path
        self.wfile.write(bytes(outstr,'utf8'))

httpd = HTTPServer(('localhost', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()

有什么想法吗?

标签: python-3.xhttpurllib

解决方案


推荐阅读