Python的urllib2的,基本的HTTP认证,并tr.im [英] Python urllib2, basic HTTP authentication, and tr.im

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

问题描述

我玩耍,尝试写一些code使用 tr.im
API来缩短的URL。

看完后 http://docs.python.org/library/urllib2.html,我想:

  TRIM_API_URL ='http://api.tr.im/api
   auth_handler = urllib2.HTTPBasicAuthHandler()
   auth_handler.add_password(境界='tr.im',
                             URI = TRIM_API_URL,
                             用户=用户名,
                             passwd文件= PASSWORD)
   首战= urllib2.build_opener(auth_handler)
   urllib2.install_opener(揭幕战)
   响应= urllib2.urlopen('%s / trim_simple?URL =%s的
                              %(TRIM_API_URL,url_to_trim))
   URL = response.read()条()

响应。code是200(我想应该是202)。网址是有效的,但
基本的HTTP验证似乎并没有工作过,因为
缩短网址是不是在我的网址(列表在 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(无,TRIM_API_URL,用户名,密码)
   auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr)
   首战= urllib2.build_opener(auth_handler)
   urllib2.install_opener(揭幕战)
   响应= urllib2.urlopen(的http://%S / trim_simple URL =%s的
                              %(TRIM_API_URL,url_to_trim))
   URL = response.read()条()

但我得到相同的结果。 (回应。code是200和url是有效的,
http://tr.im/ 不记录在我的帐户。)

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

  TRIM_API_URL ='http://api.tr.im/api
   响应= urllib2.urlopen('?%S / trim_simple URL =%S&放大器;用户名=%S&放大器;密码=%s的
                              %(TRIM_API_URL,
                                 url_to_trim,
                                 用户名,
                                 密码))
   URL = response.read()条()

...那么不仅是URL有效,但它记录在我的tr.im帐户。
(虽然响应。code仍然是200)。

有一定有毛病我code,但(而不是tr.im的API),因为

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

......返回:

<$p$p><$c$c>{\"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\",\"$c$c\":\"200\",\"message\":\"tr.im URL添加},DATE_TIME:2009-03-11T10:15:35-04:00}

...和URL会出现在我的网址名单上的http:// TR .IM /?页= 1

如果我运行:

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

......再说,我得到:

<$p$p><$c$c>{\"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\",\"$c$c\":\"201\",\"message\":\"tr.im网址已经建立了[yacitus]},DATE_TIME:2009-03-11T10:15:35-04:00}

请注意code是201,而消息是tr.im网址已创建的[yacitus]。

我不能做正确的基本HTTP认证(任一尝试)。你能找出我的问题?也许我应该看看,看看什么正在通过网络发送?我从来没有这么做过。我是否可以使用(也许在PDB)有Python的API的?还是有另一种工具(pferably $ P $适用于Mac OS X),我可​​以使用?


解决方案

这似乎运作得很好(从另一个线程取)

 进口urllib2的,BASE64请求= urllib2.Request(http://api.foursquare.com/v1/user)
base64string = base64.en codeString的('%S:%s'的%(用户名,密码))。REPLACE('\\ n','')
request.add_header(授权,基本%的%base64string)
结果= urllib2.urlopen(要求)

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

After reading http://docs.python.org/library/urllib2.html, I tried:

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

After reading http://www.voidspace.org.uk/python/articles/authentication.shtml#doing-it-properly I also tried:

   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()

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

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()

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

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

...returns:

{"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"}

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

And if I run:

$ 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"}

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

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('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)

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

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