错误-urlopen错误[Errno 8] _ssl.c:504:发生违反协议的EOF [英] Error - urlopen error [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

查看:408
本文介绍了错误-urlopen错误[Errno 8] _ssl.c:504:发生违反协议的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是在输入google搜索词后从第一页的所有链接中提取html.我在代理后面工作,所以这是我的方法.

My aim is to extract the html from all the links in the first page after entering the google search term. I work behind a proxy so this is my approach.

1.我首先使用机械化形式输入搜索词,从而正确设置了代理和机器人.

1.I first used mechanize to enter the search term in the form , ive set the proxies and robots correctly.

2.提取链接后,Ive使用了一个全局使用urllib2.ProxyHandler的打开程序来单独打开这些URL.

2.After extracting the links , Ive used an opener using urllib2.ProxyHandler globally , to open the urls individually.

但是,这给了我这个错误.无法弄清楚.

However this gives me this error. Not able to figure it out.

urlopen error [Errno 8] _ssl.c:504: EOF occurred in violation of protocol

推荐答案

您可以通过覆盖ssl_version关键字参数来在ssl模块中猴子修补ssl.wrap_socket(),而不是复制和编辑Python库模块.以下代码可以按原样使用.在发出任何请求之前,请将其放在程序的开头.

Instead of copying and editing Python library modules, you can monkey-patch ssl.wrap_socket() in the ssl module by overriding the ssl_version keyword parameter. The following code can be used as-is. Put this at the start of your program before making any requests.

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)

这篇关于错误-urlopen错误[Errno 8] _ssl.c:504:发生违反协议的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆