繁体   English   中英

Python CGI 重定向返回“标题前的脚本输出结束”

[英]Python CGI redirect returns "End of script output before headers"

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

此脚本的 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”消息。

如果需要标头,如何重定向?

您打印的所有内容都会返回到请求浏览器。 你的python文件应该是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM