【问题标题】:Python CGI redirect returns "End of script output before headers"Python CGI 重定向返回“头文件前的脚本输出结束”
【发布时间】:2021-12-13 03:10:40
【问题描述】:

我正在尝试构建一个重定向服务,让我能够跟踪我发送到国外网站的电子邮件中的点击次数。

此脚本的 URL 示例:

https://example.com/cgi-bin/redir.py?rurl=https%3A%2F%2Fwww.example.com%2Fde&utm_content=test

我在带有 cgi 模块的 Ubunut20.04 上使用 Apache2,调用以下 redir.py 脚本:

#!/usr/bin/python3

import webbrowser
  
redirect_url = "https://www.example.com"
webbrowser.open(redirect_url)

现在这会导致以下错误:

End of script output before headers: redir.py

添加标题:

print('Content-Type: text/plain')
print('')
print('hello world')

通过这个“hello world”输出,我得到了一个“hello world”消息。

如果需要header,如何重定向?

【问题讨论】:

    标签: python redirect apache2 cgi


    【解决方案1】:

    您打印的所有内容都会返回到请求浏览器。您的 python 文件应如下所示:

    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
    print("Content-Type: text/html\n\n")
    print()
    print("<meta http-equiv=\"refresh\" content=\"0; url=TARGETURL\" />")
    
    

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 2018-05-30
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-31
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多