zoukankan      html  css  js  c++  java
  • python urllib2

    import urllib2

    req=urllib2.Request('https://xxxx')

    resp=urllib2.urlopen(req)

    >>> resp=urllib2.urlopen(req)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/urllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/local/lib/python2.7/urllib2.py", line 431, in open
        response = self._open(req, data)
      File "/usr/local/lib/python2.7/urllib2.py", line 449, in _open
        '_open', req)
      File "/usr/local/lib/python2.7/urllib2.py", line 409, in _call_chain
        result = func(*args)
      File "/usr/local/lib/python2.7/urllib2.py", line 1240, in https_open
        context=self._context)
      File "/usr/local/lib/python2.7/urllib2.py", line 1197, in do_open
        raise URLError(err)
    urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>


    >>> import ssl
    >>> context=ssl._create_unverified_context()
    >>> resp=urllib2.urlopen(req,context=context)

    解决ssl 问题

    >>> resp=urllib2.urlopen(req,context=context)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python2.7/urllib2.py", line 154, in urlopen
        return opener.open(url, data, timeout)
      File "/usr/local/lib/python2.7/urllib2.py", line 437, in open
        response = meth(req, response)
      File "/usr/local/lib/python2.7/urllib2.py", line 550, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/local/lib/python2.7/urllib2.py", line 475, in error
        return self._call_chain(*args)
      File "/usr/local/lib/python2.7/urllib2.py", line 409, in _call_chain
        result = func(*args)
      File "/usr/local/lib/python2.7/urllib2.py", line 558, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 401: Authorization Required

    auth = encodestring('%s:%s'%(username,password))[:-1]
    #print auth
    req.add_header('Authorization', 'Basic %s'%auth)

    resp=urllib2.urlopen(req,context=context)

    或者

    req=urllib2.Request('https://xxxx',headers={'Authorization':'Basic %s'%auth})

    resp=urllib2.urlopen(req,context=context)

    完整代码

    import urllib2
    from base64 import encodestring
    import ssl
    
    username='xxx'
    password='xxxx'
    
    auth = encodestring('%s:%s'%(username,password))[:-1]
    
    req=urllib2.Request('https:/xxxx',headers={'Authorization':'Basic %s'%auth})
    
    context=ssl._create_unverified_context()
    
    resp=urllib2.urlopen(req,context=context)
  • 相关阅读:
    Oracle——oem开启managementserver的时候搜索不到节点的解决方法!!!
    PL/SQL学习笔记(一)
    oracle 数据库的非指令备份方法
    OracleDBConsole[SID]服务简介
    Toad 使用快速入门
    oracle 配置
    sql中exist与in 的区别
    页面之间传递参数的几种方法荟萃
    div+CSS网页布局入门系列教程(来自标准之路)
    XML操作类
  • 原文地址:https://www.cnblogs.com/nevermore29/p/11890158.html
Copyright © 2011-2022 走看看