如何检查401的HTTP状态码? [英] How to check for an HTTP status code of 401?

查看:199
本文介绍了如何检查401的HTTP状态码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我收到的答案之一中,我遇到了一个问题,就是不知道如何通过Google App Engines自动将我的ID和密码传递给网站,我是注册用户并拥有一个帐户。我给了我一个建议:检查401的HTTP状态代码,需要授权,并提供网站要求的HTTP授权(基本,摘要,无论什么)。我不知道如何检查状态码。任何人都可以,请告诉我该怎么做?

+++++++++++++++++++++++++++++++++

  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:
printcontent-type:text / plain
print
print result.status_code


$ b $在普通的Python代码中,我可能会使用200而不是401。

低级 httplib ,例如:

 进口httplib 
$ b $域名='google.com gmail.com appspot.com'.split()

域中的域:
conn = httplib.HTTPConn ection(domain)
conn.request('GET','/')
resp = conn.getresponse()
print'%r'%(resp.status,域名)

这会显示你的代码为301(永久移动)和302(暂时移动);更高级别的库(如 urllib2 )会为你幕后处理这些事情,这很方便,但却让你更难控制简单性(你会有安装你自己的url opener对象等等)。

在App Engine中,你最好使用 urlfetch ,它会返回响应对象 status_code 属性。如果该属性为401,则意味着您需要在

中使用适当类型的授权信息来重复获取

然而,现在,App Engine也支持urllib2,所以如果您愿意使用这种更高级别的抽象,您可以将工作委托给它。请参阅此处获取有关如何将基本身份验证委派给urllib2和这里,获得更基本认证如何工作的一般教程(我相信,即使您使用更高层,了解底层抽象层的情况也会起到帮助作用!)。


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"

解决方案

In ordinary Python code, I'd probably use the lower-level httplib, e.g.:

import httplib

domains = 'google.com gmail.com appspot.com'.split()

for domain in domains:
  conn = httplib.HTTPConnection(domain)
  conn.request('GET', '/')
  resp = conn.getresponse()
  print 'Code %r from %r' % (resp.status, domain)

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!-).

这篇关于如何检查401的HTTP状态码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆