需要HTTP鉴权的URL请求

该例子来源于工作中遇到的情况:

需要访问一个url去获取某个owner所拥有的资源情况。但是在访问之前需要用basic authentication 进行身份认证。

 

查了很久,由于对http相关的东西不是特别熟悉。但是知道perl中有个LWP的模块可以直接鉴权后进行url访问

如下:my $userAgent = LWP::UserAgent->new();
$userAgent->cookie_jar({});
$userAgent->credentials(
   'host',
   'w3',
   'user' => &password
);

于是想到python中应该有有类似的模块,于是找到了urllib2 和LWPCookieJar

 

具体实现代码如下:

 

from optparse import OptionParser
import getpass
import httplib
import base64
import string
import httplib
import urllib2
import urllib
import  re
import  sys, cookielib,  os, json


def getCommand():

   

    TRIM_API_URL = 'your url'
    h = urllib2.HTTPPasswordMgrWithDefaultRealm()


    h.add_password(None,
                        uri=TRIM_API_URL,
                        user='your user name',
                        passwd='your password')

    auth_handler=urllib2.HTTPBasicAuthHandler(h)
    cookieFile = "cookies.dat"
    cJar = cookielib.LWPCookieJar()

    #??HTTPCookieProcessor?opener??

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cJar),auth_handler)#这里我出的一个错就是不知道build_opener可以传多个参数,开始怎么也不能鉴权通过,后来加入了auth_handler参数
    #opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
   
    try:
        re=urllib2.urlopen('your login url',None,timeout=3600)
        url = re.read().strip()
        print url

    except urllib2.HTTPError, e:
        print e.code
        print e.header
       
    for ind, cookie in enumerate(cJar):#为了方便理解,将cookie信息打印出来

        print "%d - %s" % (ind, cookie)
    print "test"
    cJar.save(cookieFile)
    #??cookies
   
    re=urllib2.urlopen('access url after log in')#此时访问便不再需要login cookie会自动记录相关信息
    url = re.read().strip()
    print url



     
if __name__ == '__main__':
    getCommand()

 

关于:

class cookielib. LWPCookieJar ( filename , delayload=None , policy=None )
A FileCookieJar that can load from and save cookies to disk in format compatible with the libwww-perl library’s Set-Cookie3 file format. This is convenient if you want to store cookies in a human-readable file.

urllib2. build_opener ( [ handler , ... ] )

Return an OpenerDirector instance, which chains the handlers in the order given. handler s can be either instances of BaseHandler , or subclasses of BaseHandler (in which case it must be possible to call the constructor without any parameters). Instances of the following classes will be in front of the handler s, unless the handler s contain them, instances of them or subclasses of them: ProxyHandler , UnknownHandler , HTTPHandler , HTTPDefaultErrorHandler , HTTPRedirectHandler , FTPHandler , FileHandler , HTTPErrorProcessor .

 

 

转载一段关于urllib2:

urllib2是python 的一个获取url(Uniform Resource
Locators,统一资源定址器)的模块。它用urlopen函数的形式提供了一个非常简洁的接口。这使得用各种各样的协议获取url成为可能。它同时
也提供了一个稍微复杂的接口来处理常见的状况-如基本的认证,cookies,代理,等等。这些都是由叫做opener和handler的对象来处理的。
以下是获取url最简单的方式:
import urllib2
response = urllib2.urlopen('http://python.org/')
html = response.read()
许多urlib2的使用都是如此简单(注意我们本来也可以用一个以”ftp :”"file:”等开头的url取代”HTTP”开头的url).然
而,这篇教程的目的是解释关于HTTP更复杂的情形。HTTP建基于请求和回应(requests &responses
)-客户端制造请求服务器 返回回应。urlib2用代 表了你正在请求的HTTP
request的Request对象反映了这些。用它最简单的形式,你建立了一个Request对象来明确指明你想要获取的url。调用urlopen函
数对请求的url返回一个respons对象。这个respons是一个像file的对象,这意味着你能用.read()函数操作这个respon对象:
import urllib2
req = urllib2.Request('http://www.voidspace.org.uk')
response = urllib2.urlopen(req)
the_page = response.read()

 

更多信息参考python文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惹不起的程咬金

来都来了,不赏点银子么

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值