从Python CGI脚本发送301/302重定向

问题描述 投票:0回答:1

我一直在寻找如何从Python之外的HTTP客户端响应中读取重定向的答案,但我不知道如何将301或302重定向发送回客户端。

等效的PHP代码将是:

// Redirect the browser to Google
header("Location: https://www.google.com/", true, 301);
python http-headers cgi http-status-code-301 http-status-code-302
1个回答
0
投票

以最简单的形式,没有外部库,您只需输出直接的HTTP:

print('''Status: 301 Redirect
Location: {url}
Content-Type: text/html

Moved permanently to <a href="{url}">{url}</a>
'''.format(url='https://www.google.com/'))

如果您使用的是库或框架,则可能有更好的方法。

© www.soinside.com 2019 - 2024. All rights reserved.