如何检查401的HTTP状态码? [英] How to check for an HTTP status code of 401?
问题描述
在我收到的答案之一中,我遇到了一个问题,就是不知道如何通过Google App Engines自动将我的ID和密码传递给网站,我是注册用户并拥有一个帐户。我给了我一个建议:检查401的HTTP状态代码,需要授权,并提供网站要求的HTTP授权(基本,摘要,无论什么)。我不知道如何检查状态码。任何人都可以,请告诉我该怎么做? +++++++++++++++++++++++++++++++++ 如果我在Google App Engine中使用这种方式(获取我的易趣摘要页面的网址),请点击 :
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
$ 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,则意味着您需要在
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屋!