【问题标题】:Returning http status codes in Python CGI在 Python CGI 中返回 http 状态码
【发布时间】:2010-10-24 10:32:34
【问题描述】:

是否可以通过python cgi脚本发送200以外的状态码(如301重定向)

【问题讨论】:

    标签: python http cgi


    【解决方案1】:

    通过 cgi 脚本?

    print "Status:301\nLocation: http://www.google.com"
    

    【讨论】:

    • 在这种情况下你也可以发布http请求处理程序/
    • 还要注意,如果您通过 Python 的内置 CGIHTTPServer stackoverflow.com/a/32079589/227651 运行脚本,这将不起作用
    • @MikeHowsden 好点!似乎无法使用 python 内置的 CGIHTTPServer 返回状态代码。 IMO 这是一个主要缺点,因为即使在非常简单的 web 应用程序中,返回不同的状态也很常见。幸运的是,其他 Web 服务器的行为并不相同。
    【解决方案2】:

    通过 wsgi 应用程序?

    def simple_app(environ, start_response):
        status = '301 Moved Permanently' # HTTP Status
        headers = [('Location','http://example.com')] # HTTP Headers
        start_response(status, headers)
    
        return []
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2013-11-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2012-02-12
      • 1970-01-01
      相关资源
      最近更新 更多