Python Mechanise urllib2.URLError违反协议 - 处理https?

时间:2015-02-17 15:04:39

标签: python https urllib2 mechanize

我正在尝试访问https://地址。

测试代码有效:

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.set_handle_refresh(False)
br.addheaders = [('User-agent', 'Firefox')]

response = br.open("http://www.google.com")
for f in br.forms():
    print f

当我使用https://secure.crbonline.gov.uk/enquiry/enquirySearch.do代替www.google.com运行相同的代码时,控制台会抛出以下错误:

Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/dbs/website.py", line 11, in <module>
    response = br.open("https://secure.crbonline.gov.uk/enquiry/enquirySearch.do")
  File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 203, in open
    return self._mech_open(url, data, timeout=timeout)
  File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 230, in _mech_open
    response = UserAgentBase.open(self, request, data)
  File "C:\Python27\lib\site-packages\mechanize\_opener.py", line 193, in open
    response = urlopen(self, req, data)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 344, in _open
    '_open', req)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 332, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1170, in https_open
    return self.do_open(conn_factory, req)
  File "C:\Python27\lib\site-packages\mechanize\_urllib2_fork.py", line 1118, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 8] _ssl.c:510: EOF occurred in violation of protocol>

根据我的理解,处理HTTPS连接存在问题。

我尝试在br.set_proxies之前插入response语句,通过代理运行上述操作,但这并没有解决问题。

任何帮助实现这项工作将不胜感激。

2 个答案:

答案 0 :(得分:1)

跟随马克的建议,找到了这个片段

import ssl
from functools import wraps
def sslwrap(func):
    @wraps(func)
    def bar(*args, **kw):
        kw['ssl_version'] = ssl.PROTOCOL_TLSv1
        return func(*args, **kw)
    return bar

ssl.wrap_socket = sslwrap(ssl.wrap_socket)

response之前粘贴它,它现在有效!

答案 1 :(得分:0)

使用新的mechanize v0.3.5和新的python v2.7.9 +,你可以在br.open()之前使用以下代码

br = mechanize.Browser()
br.set_ca_data(context=ssl.SSLContext(ssl.PROTOCOL_TLSv1))
...
br.open("...")