标签:ref self 文件 时间 err chain text 遇到 www
今天发现平时每小时报表内容少了好几个table ,感觉好奇怪,这个代码很稳定跑了好长时间了,不知道哪里出来问题,幸亏代码做了异常处理。否则我估计邮件都会发不出来了,看了下日志文件,有报错日志记录如下:
` page = urllib2.urlopen(url, timeout=300)
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)
URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>
2018-11-13 07:03:27,612 - <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:
581)>
2018-11-13 08:03:10,058 - <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:
581)>`
我把服务器上的代码拿到本地跑没问题,但是放到服务器上却报错,正是邪门了,百度下,有人遇到过同样的问题,下面贴出别人的解决方案
`
1. 使用ssl创建未经验证的上下文,在urlopen中传入上下文参数
import ssl
import urllib2
context = ssl._create_unverified_context()
print urllib2.urlopen("https://www.12306.cn/mormhweb/", context=context).read()
ssl._create_default_https_context = ssl._create_unverified_context
print urllib2.urlopen("https://www.12306.cn/mormhweb/").read()
注意:在全全局请求文件导入import ssl
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
`
我只测试了第一种,代码该了之后,再服务器上跑了一下,解决问题
标签:ref self 文件 时间 err chain text 遇到 www
原文地址:http://blog.51cto.com/world77/2319518