【问题标题】:How do I set the content-type when using python's http server?使用python的http服务器时如何设置content-type?
【发布时间】:2011-12-18 18:40:12
【问题描述】:

我尝试使用send_header('Content-Type', 'text/html')。但是,这不起作用。

【问题讨论】:

    标签: python content-type httpserver


    【解决方案1】:

    以下在派生自http.server.BaseHTTPRequestHandler 的类中对我有用:

    self.send_response(200)
    self.send_header("Content-type", "text/plain")
    self.end_headers()
    # Your response body follows with self.wfile.write()
    

    查看更多 herehere

    【讨论】:

      【解决方案2】:

      可能需要双引号?见example。我希望这会有所帮助。

      【讨论】:

      • 我尝试了双引号,但这也不起作用。似乎起作用的是“content_type”而不是“Content-Type”(使用下划线而不是连字符)。但是,我需要将其作为连字符,以便其他一些依赖“Content-Type”的程序可以工作。
      • 您可能想查看响应中的实际标头:即使您在 API 调用中将其设置为 content_type,它也可以正确使用实际标头中的 - 版本
      猜你喜欢
      • 1970-01-01
      • 2014-09-13
      • 2011-01-04
      • 2020-04-16
      • 2010-12-18
      • 2016-11-04
      • 2010-12-08
      • 1970-01-01
      • 2016-10-02
      相关资源
      最近更新 更多