python认为http响应代码是200,但服务器说301

2024-06-10 10:54:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是python3urllib.request.urlopen()功能。在过去,它运作得很好。事实上,我几个月前在同一个程序中用过它,效果很好。但是现在,每当我尝试使用api在我的站点上发布时,服务器就会记录301个响应。当我使用响应.getcode()为了找到答案,它说是200。在

什么会导致这种差异?有没有其他方法来检查我的请求是否失败,或者有一种方法来调试它?我个人没有访问服务器的权限,但是我可以让管理员帮我检查一下。在

谢谢各位!在

编辑

我在wireshark中找到了HTTP请求。它发送一个POST请求,得到301(text/html),然后发送一个GET请求,得到200(application/json)。这是什么意思?我最初的请求是json(我使用urllib.request.urlopen(url,数据)。POST to http://sefaria.org/api/texts/Rashi_on_Berakhot.2a.1.1的第一个响应是:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
    <head>
    <title>301 Moved Permanently</title>
    </head>
    <body>
        <h1>Moved Permanently</h1>
        <p>The document has moved <a href="http://www.sefaria.org/api/texts/Rashi_on_Berakhot.2a.1.1">here</a>.</p>\n
        <hr>
        <address>Apache/2.4.6 (Ubuntu) Server at sefaria.org Port 80</address>
    </body>
</html>

该url的GET请求响应为200


Tags: 方法org服务器apijsonhttpurlget
2条回答

原来我发的是http://website.com而不是{a2}。。。 哎哟。在

这不是遇到301时的预期行为:

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

您应该考虑将其作为bug报告给Python开发人员。在

相关问题 更多 >