重定向url只打印到屏幕上 - Python

3

我编写了一个简单的Python脚本,用于在点击广告后重定向URL。
与广告相关的URL是http://server.example.com/redirect.py

redirect.py的代码如下所示:

import cgi
import cgitb

cgitb.enable()
print('Content-Type: text/html\n\n')
print('Location: http://www.finalurl.com')

然而,当用户点击广告时,请求并没有被重定向到www.finalurl.com,而是直接在浏览器窗口中显示了最终的网址。非常感谢您提供的所有帮助。
请注意,redirect.py脚本位于/public_html/cgi-bin/文件夹中,并且该脚本具有711权限。此外,目标网址并不总是相同的,它基于数据库查询结果而变化。该代码被简单地省略了。
我还尝试包含以下内容:print ("Status: 301 Moved")

如果您认为这个解决方案可以解决您的问题,请选择它作为接受的答案。 - Nilesh
1个回答

4
'

'\n\n'

'是http头部结束标志,应该使用'

'\r\n\r\n'

'代替:'
print 'Status: 301 Moved Permanently'
print 'Location: http://www.finalurl.com'
print 'Content-Type: text/html'
print # end of header
... # short html with the url

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接