ImportError:无法导入名称CertificateError

时间:2017-01-24 20:08:23

标签: python python-2.7

以下代码会引发以下错误:

Traceback (most recent call last):
  File "./python_PAN_traffic_scraper.py", line 4, in <module>
    import urllib3
  File "/usr/lib/python2.7/site-packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/usr/lib/python2.7/site-packages/urllib3/connectionpool.py", line 29, in <module>
    from ssl import CertificateError
ImportError: cannot import name CertificateError

我做错了什么?我想浏览到防火墙门户界面,该界面使用自签名证书,并在尝试使用特定API URL和API密钥浏览到站点时抛出证书错误。最后,我想扩展代码以解析XML以查找特定的标记和属性(源/目标/端口/等)。

#!/usr/bin/env python
import ssl
import urllib
import urllib3
import xml.etree.ElementTree as ET

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

DAL = "1.1.1.1"
CHI = "2.2.2.2"

firewall_ip = [ 'Dallas', 'Chicago' ]

datacenter=raw_input('Which firewall do you want traffic logs for? \"DAL\" or \"CHI\"?: ')

contents = ""

cmd = "/api/?type=log&log-type=traffic&query=(receive_time geq '2017/1/22 23:00:00')"
key = "&key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa="
url = "https://"+pan_ip[0]+urllib3.urlencode(cmd)+key

response = urllib3.urlopen(url, verify=False)
contents = ET.fromstring(response.read())


if DC is "DAL":
    print contents
else:
    print "Goodbye"

#result = ""



#for i in contents[0][0]:
    #   if i[0][0].tag == entry:
    #       for j in i[0][0]:
    #           if j.tag == 'src' and j.text == src:
    #               result = i.attrib['name']
    #print result

1 个答案:

答案 0 :(得分:0)

我卸载了urllib3并进行了全新安装:

$ git clone git://github.com/shazow/urllib3.git
cd to the folder containing urllib3/
$ python setup.py install

这完全摆脱了错误。