簡體   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