Python urllib2、基本 HTTP 身份验证和 tr.im [英] Python urllib2, basic HTTP authentication, and tr.im

查看:29
本文介绍了Python urllib2、基本 HTTP 身份验证和 tr.im的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩,试图编写一些代码来使用 tr.im用于缩短网址的 API.

I'm playing around, trying to write some code to use the tr.im APIs to shorten a URL.

阅读http://docs.python.org/library/urllib2.html,我试过了:

   TRIM_API_URL = 'http://api.tr.im/api'
   auth_handler = urllib2.HTTPBasicAuthHandler()
   auth_handler.add_password(realm='tr.im',
                             uri=TRIM_API_URL,
                             user=USERNAME,
                             passwd=PASSWORD)
   opener = urllib2.build_opener(auth_handler)
   urllib2.install_opener(opener)
   response = urllib2.urlopen('%s/trim_simple?url=%s'
                              % (TRIM_API_URL, url_to_trim))
   url = response.read().strip()

response.code 是 200(我认为应该是 202).网址有效,但基本的 HTTP 身份验证似乎不起作用,因为缩短的 URL 不在我的 URL 列表中(位于 http://tr.im/?page=1).

response.code is 200 (I think it should be 202). url is valid, but the basic HTTP authentication doesn't seem to have worked, because the shortened URL isn't in my list of URLs (at http://tr.im/?page=1).

阅读http://www.voidspace 后.org.uk/python/articles/authentication.shtml#doing-it-properly我也试过:

   TRIM_API_URL = 'api.tr.im/api'
   password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
   password_mgr.add_password(None, TRIM_API_URL, USERNAME, PASSWORD)
   auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
   opener = urllib2.build_opener(auth_handler)
   urllib2.install_opener(opener)
   response = urllib2.urlopen('http://%s/trim_simple?url=%s'
                              % (TRIM_API_URL, url_to_trim))
   url = response.read().strip()

但我得到了相同的结果.(response.code 为 200,url 有效,但没有记录在我的 http://tr.im/ 帐户中.)

But I get the same results. (response.code is 200 and url is valid, but not recorded in my account at http://tr.im/.)

如果我使用查询字符串参数而不是基本的 HTTP 身份验证,像这样:

If I use query string parameters instead of basic HTTP authentication, like this:

   TRIM_API_URL = 'http://api.tr.im/api'
   response = urllib2.urlopen('%s/trim_simple?url=%s&username=%s&password=%s'
                              % (TRIM_API_URL,
                                 url_to_trim,
                                 USERNAME,
                                 PASSWORD))
   url = response.read().strip()

...那么不仅 url 有效,而且还记录在我的 tr.im 帐户中.(虽然 response.code 仍然是 200.)

...then not only is url valid but it's recorded in my tr.im account. (Though response.code is still 200.)

我的代码肯定有问题(而不是 tr.im 的 API),因为

There must be something wrong with my code though (and not tr.im's API), because

$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk

...返回:

{"trimpath":"hfhb","reference":"nH45bftZDWOX0QpVojeDbOvPDnaRaJ","trimmed":"11/03/2009","destination":"http://www.google.co.uk/","trim_path":"hfhb","domain":"google.co.uk","url":"http://tr.im/hfhb","visits":0,"status":{"result":"OK","code":"200","message":"tr.im URL Added."},"date_time":"2009-03-11T10:15:35-04:00"}

...并且该 URL 确实出现在我在 http://tr 上的 URL 列表中.im/?page=1.

...and the URL does appear in my list of URLs on http://tr.im/?page=1.

如果我跑:

$ curl -u yacitus:xxxx http://api.tr.im/api/trim_url.json?url=http://www.google.co.uk

...再次,我明白了:

...again, I get:

{"trimpath":"hfhb","reference":"nH45bftZDWOX0QpVojeDbOvPDnaRaJ","trimmed":"11/03/2009","destination":"http://www.google.co.uk/","trim_path":"hfhb","domain":"google.co.uk","url":"http://tr.im/hfhb","visits":0,"status":{"result":"OK","code":"201","message":"tr.im URL Already Created [yacitus]."},"date_time":"2009-03-11T10:15:35-04:00"}

注释代码为 201,消息为tr.im URL Already Created [yacitus]."

Note code is 201, and message is "tr.im URL Already Created [yacitus]."

我一定没有正确地进行基本的 HTTP 身份验证(无论是哪种尝试).你能看出我的问题吗?也许我应该查看一下通过线路发送的内容?我以前从未这样做过.是否有我可以使用的 Python API(可能在 pdb 中)?或者我可以使用其他工具(最好是 Mac OS X)吗?

I must not be doing the basic HTTP authentication correctly (in either attempt). Can you spot my problem? Perhaps I should look and see what's being sent over the wire? I've never done that before. Are there Python APIs I can use (perhaps in pdb)? Or is there another tool (preferably for Mac OS X) I can use?

推荐答案

这似乎工作得很好(摘自另一个线程)

This seems to work really well (taken from another thread)

import urllib2, base64

request = urllib2.Request("http://api.foursquare.com/v1/user")
base64string = base64.encodestring('%s:%s' % (username, password)).replace('
', '')
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

这篇关于Python urllib2、基本 HTTP 身份验证和 tr.im的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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