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)
  • 相关阅读:
    Mono和IL2Cpp
    axios无法获取响应头headers的ContentDisposition
    elcascader(联机选择器)动态加载+编辑默认值回显
    Vue ElTree 拖拽排序方法(通用)
    Postman保存token并使用token的整个流程
    python 使用exec执行定义好的方法,提示“name 'XXX' is not defined”
    Python+flask+flaskapscheduer实现定时下发任务
    androidtools下的uiautomatorviewer截图,提示“Unexpected error while obtaining UI hierarchy”
    python 插入mysql数据库字符串中含有单引号或双引号报错
    python 根据传进来的参数,动态拼接sql
  • 原文地址:https://www.cnblogs.com/nevermore29/p/11890158.html
Copyright © 2011-2022 走看看