【发布时间】:2015-10-21 16:24:19
【问题描述】:
我在使用 urllib2 读取网站时收到“WindowsError:[错误 5] 访问被拒绝”消息。
from urllib2 import urlopen, Request
from bs4 import BeautifulSoup
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'}
req = Request('https://' + url, headers=hdr)
soup = BeautifulSoup( urlopen( req ).read() )
完整的回溯是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 431, in open
response = self._open(req, data)
File "C:\Python27\lib\urllib2.py", line 449, in _open
'_open', req)
File "C:\Python27\lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 1240, in https_open
context=self._context)
File "C:\Python27\lib\urllib2.py", line 1166, in do_open
h = http_class(host, timeout=req.timeout, **http_conn_args)
File "C:\Python27\lib\httplib.py", line 1258, in __init__
context = ssl._create_default_https_context()
File "C:\Python27\lib\ssl.py", line 440, in create_default_context
context.load_default_certs(purpose)
File "C:\Python27\lib\ssl.py", line 391, in load_default_certs
self._load_windows_store_certs(storename, purpose)
File "C:\Python27\lib\ssl.py", line 378, in _load_windows_store_certs
for cert, encoding, trust in enum_certificates(storename):
WindowsError: [Error 5] Access is denied
按照here 的建议,我已尝试从具有管理员权限的命令提示符运行脚本,但它并不能解决问题。
关于如何解决此错误的任何建议?
【问题讨论】:
-
您是否正在运行 web2py 的源版本,并且您是否能够在 web2py 上下文之外发出相同的请求(例如,来自标准 Python shell 或 Python 脚本)?
-
从外观上看,Windows 拒绝访问证书存储区。
-
你可以尝试安装
certifi吗? -
@Boa - 你能确认这个 sn-p 触发了你的问题吗?
from _ssl import enum_certificates; print enum_certificates("ROOT"); print enum_certificates("CA")。如果有,是哪一个,还是两者都有? -
所以,Windows
CA-certification authority存储 (certmgr:Intermediate Certification Authorities) 出现问题,导致WINAPI::CertOpenSystemStore对CA失败。我建议启动certmgr,导航到Intermediate Certification Authorities并浏览该证书列表或与微软支持部门联系,因为那里似乎有什么问题。无论如何,可以检查print enum_certificates(u"CA")是否也引发错误。