如何检查 HTTP 状态码是否为 401?
在我收到的其中一个答案中,我遇到了一个问题,即不知道如何通过“Google App Engines”自动将我的 ID 和密码传递到我所在的网站注册用户并拥有帐户。有人向我建议“检查 HTTP 状态代码 401,“需要授权”,并提供网站要求的 HTTP 授权类型(基本、摘要等)”。我不知道如何检查状态代码。请问有人可以告诉我该怎么做吗?
+++++++++++++++++++++++++++++++++++
附加信息:
如果我在 Google App Engine 中使用这种方式(获取 url我的 eBay 摘要页面):
from google.appengine.api import urlfetch
url = "http://my.ebay.com/ws/eBayISAPI.dll?MyEbay&gbh=1&CurrentPage=MyeBaySummary&ssPageName=STRK:ME:LNLK"
result = urlfetch.fetch(url)
if result.status_code == 200:
print "content-type: text/plain"
print
print result.status_code
我总是得到“200”而不是“401”
In one of the answers that I have received here, I encountered a problem of not knowing how to pass automatically through "Google App Engines" my ID and a password to a website, on which I am a registered user and have an account. A suggestion was given to me to "check for an HTTP status code of 401, "authorization required", and provide the kind of HTTP authorization (basic, digest, whatever) that the site is asking for". I don't know how to check for status code. Can anyone, please, tell me how to do it?
+++++++++++++++++++++++++++++++++
Additional Information:
If I use this way in Google App Engine (fetching the url of my eBay summary page):
from google.appengine.api import urlfetch
url = "http://my.ebay.com/ws/eBayISAPI.dll?MyEbay&gbh=1&CurrentPage=MyeBaySummary&ssPageName=STRK:ME:LNLK"
result = urlfetch.fetch(url)
if result.status_code == 200:
print "content-type: text/plain"
print
print result.status_code
I always get "200" instead of "401"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在普通的Python代码中,我可能会使用较低级别的httplib,例如:
这将显示 301(永久移动)和 302(临时移动)等代码;诸如 urllib2 之类的更高级别的库将为您处理“幕后”的此类事情,这很方便,但使您更难简单地进行控制(您必须安装自己的“url开启者”对象等)。
在 App Engine 中,您最好使用 urlfetch ,它返回一个带有
status_code 的响应对象
属性。如果该属性为 401,则意味着您需要使用标头中适当类型的授权信息重复获取。不过,App Engine 现在也支持 urllib2,因此,如果您习惯使用这种更高级别的抽象,则可以将工作委托给它。有关如何将基本身份验证委托给urllib2 和此处了解有关基本身份验证如何工作的更一般教程(我相信,即使您使用的是较高层,了解较低抽象层发生的情况也会对您有所帮助!-)。
In ordinary Python code, I'd probably use the lower-level httplib, e.g.:
this will show you such codes as 301 (moved permanently) and 302 (moved temporarily); higher level libraries such as
urllib2
would handle such things "behind the scenes" for you, which is handy but makes it harder for you to take control with simplicity (you'd have to install your own "url opener" objects, etc).In App Engine, you're probably better off using urlfetch, which returns a response object with a
status_code
attribute. If that attribute is 401, it means that you need to repeat the fetch with the appropriate kind of authorization information in the headers.However, App Engine now also supports urllib2, so if you're comfortable with using this higher level of abstraction you could delegate the work to it. See here for a tutorial on how to delegate basic authentication to urllib2, and here for a more general tutorial on how basic authentication works (I believe that understanding what's going on at the lower layer of abstraction helps you even if you're using the higher layer!-).
除非我不完全理解您的问题,否则您可以从
首先,您必须发出 fetch()到您要测试的 URL。
Unless I don't understand fully your question, you can grab the return code from the Response Object using the
status_code
property.First, you'll have to issue a fetch() to the URL you want to test.
大多数面向用户的网站不使用 HTTP 身份验证,而是更喜欢使用基于 cookie 的身份验证,并使用 HTML 表单进行登录。如果您想在自己的代码中复制此内容,则需要向相关应用程序的登录 URL 发出 HTTP POST 请求,并捕获发回的 cookie,包括在您将来验证自己身份的所有请求中的 cookie。如果没有有关您尝试对其进行身份验证的特定站点的更多详细信息,则很难更具体。
Most user-oriented sites don't use HTTP authentication, preferring instead to use cookie-based authentication, with HTML forms for signin. If you want to duplicate this in your own code, you need to make an HTTP POST request to the login URL for the application in question, and capture the cookie that's sent back, including that in all your future requests to authenticate yourself. Without more details about the specific site you're trying to authenticate against, it's difficult to be more specific.
您不会收到 401,因为该网站始终返回 200,而不是 401。通常我们为网站所做的编码类型是返回 200,页面上写着“请登录..blah blah”,如果网站返回 200 以外的任何内容,浏览器将不会显示时髦的错误消息。
简而言之,正如我在其他问题中提到的,您需要查看登录页面,查看它使用的参数,例如登录 = xxx,密码 = yyy,将其发布到该页面,您也必须管理 cookie,这就是像斜纹布等库就出现了。
You are not getting 401 because that site is not returning 401 but 200 always. Usually type of coding we do for websites is return 200 with a page saying "Please login..blah blah", if site returned anything other then 200 browser will not display the funky error msg.
So in short as i mentioned in other question, you need to look into login page, see what params it uses e.g login=xxx, password=yyy, post it to that page and you will have to manage the cookies too, that is where library like twill etc come into picture.