简体   繁体   English

Python重定向到URL不起作用

[英]Python redirect to URL not working

I know there are other posts on this, but I've tried everything I can think of and still can't get a simple redirect working as a CGI script on a Linux server. 我知道这方面还有其他文章,但是我已经尝试了所有可以想到的方法,但仍然无法在Linux服务器上作为CGI脚本使用简单的重定向。 I've tried 3 different methods and none of them works. 我尝试了3种不同的方法,但没有一种有效。 I know some people will suggest removing the first two print statements after the import, but that gives me 500 Server error. 我知道有人会建议在导入后删除前两个打印语句,但这会给我500服务器错误。 All I'm trying to do is have the browser redirect to another URL when this python CGI script is called. 我要做的就是在调用此python CGI脚本时将浏览器重定向到另一个URL。

#!/usr/bin/env python
import cgi, cgitb 
print "Content-Type: text/html"
print

def go_to_url(url):
    print "HTTP/1.1 302 Found"
    print "Location: ",url,"\r\n"
    print "Connection: close \r\n"
    print " "

def go_to_url2(url):
    print ('<script type="text/javascript">window.location = ' + url + ';</script>')

def go_to_url3(url):
    print '<html><head><script type="text/javascript">'
    print '<!-- function jump(){window.location = "' + url + '" } //-->'
    print '</script></head><body onLoad="jump()"></body></html>'

url = 'http://www.yahoo.com'
# go_to_url(url)    
# go_to_url2(url)   
go_to_url3(url)

I solved it. 我解决了 This works. 这可行。 Other important problems to note, 1. when developing on a Windows system make sure to change the line endings to Linux format 2. chmod the python file to 755. 其他需要注意的重要问题:1.在Windows系统上进行开发时,请确保将行尾更改为Linux格式。2.将python文件chmod更改为755。

def go_to_url(url):
    print ('<html><header><script type="text/javascript">window.location ="' + url + '";</script></header><body><p>Redirect2</p></body></html>')

First, it's important to keep in mind that the HTTP version should be dynamic by the client's browser, you shouldn't force it. 首先,请务必牢记,客户端的浏览器应将HTTP版本动态化,因此您不应强行使用它。

Javascript solution for HTTP redirect creates more overhead in performance. 用于HTTP重定向的Javascript解决方案会增加性能开销。

Because 1st request is for an HTML page with the JS redirect code, then a 2nd request is for the redirect itself ( two requests in total vs one ) 因为第一个请求是针对带有JS重定向代码的HTML页面,所以第二个请求是针对重定向本身的(总共两个请求vs一个)

I believe you can't redirect after you've printed print "Content-Type: text/html" . 我相信您在print "Content-Type: text/html"后无法重定向。

Your script should look something like this: 您的脚本应如下所示:

#!/usr/bin/env python
import cgi 

def print_http_header():
    print "Content-type: text/html; charset=UTF-8"
    print

def go_to_url(url):
    # HTTP version ( 1.0 / 1.1 / 2.0 ) should be determine by browser
    print "Status: 302 Moved"
    print "Location: %s" % url
    print

def go_to_url2(url):
    print_http_header()
    print ('<script type="text/javascript">window.location = ' + url + ';</script>')

def go_to_url3(url):
    print_http_header()
    print '<html><head><script type="text/javascript">'
    print '<!-- function jump(){window.location = "' + url + '" } //-->'
    print '</script></head><body onLoad="jump()"></body></html>'

url = 'http://www.yahoo.com'
# go_to_url(url)    
# go_to_url2(url)   
go_to_url3(url)

Hope that helps.... 希望有帮助...

I know this is old, but I hope this helps. 我知道这很老了,但希望对您有所帮助。 It worked for me! 它为我工作!

print ("Status: 302 Moved")
print ("Location: http://www.somedomain.com/")
print() # HTTP says you have to have a blank line between headers and content

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

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