Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 python SSLError(“错误握手:SysCallError(-1,';意外EOF';)”,),)_Python 2.7_Beautifulsoup_Openssl_Python Requests - Fatal编程技术网

Python 2.7 python SSLError(“错误握手:SysCallError(-1,';意外EOF';)”,),)

Python 2.7 python SSLError(“错误握手:SysCallError(-1,';意外EOF';)”,),),python-2.7,beautifulsoup,openssl,python-requests,Python 2.7,Beautifulsoup,Openssl,Python Requests,我正在抓取这个aspx网站 根据需要,在发送post请求时,我必须在\uuu VIEWSTATE和\uuu EVENTVALIDATION中进行解析。现在,我尝试先发送一个get请求来获取这两个值,然后再进行解析 但是,我已多次尝试发送get请求。结果总是抛出以下错误消息: requests.exceptions.SSLError:HTTPSConnectionPool(host='gra206.aca.ntu.edu.tw',port=443):url:/Temp/W2.aspx?Type=2

我正在抓取这个aspx网站

根据需要,在发送post请求时,我必须在\uuu VIEWSTATE\uuu EVENTVALIDATION中进行解析。现在,我尝试先发送一个get请求来获取这两个值,然后再进行解析

但是,我已多次尝试发送get请求。结果总是抛出以下错误消息:

requests.exceptions.SSLError:HTTPSConnectionPool(host='gra206.aca.ntu.edu.tw',port=443):url:/Temp/W2.aspx?Type=2超出了最大重试次数(由SSLError引起(SSLError(“错误握手:系统调用错误(-1,'意外EOF'),),)

我试过:

  • 升级OpenSSL
  • 下载请求[安全性]
  • 然而,它们都不起作用

    我目前正在使用:

    env:
    python 2.7
    bs4 4.6.0
    request 2.18.4
    openssl 1.0.2n
    
    这是我的密码:

    import requests
    from   bs4 import BeautifulSoup
    
    with requests.Session() as s:
        s.auth = ('user', 'pass')
        s.headers.update({'x-test': 'true'})
        url = 'https://gra206.aca.ntu.edu.tw/Temp/W2.aspx?Type=2'
        r = s.get(url, headers={'x-test2': 'true'})
    
    soup = BeautifulSoup(r.content, 'lxml')
    viewstate  = soup.find('input', {'id': '__VIEWSTATE'         })['value']
    validation = soup.find('input', {'id': '__EVENTVALIDATION'   })['value']  
    print viewstate, generator, validation
    

    我也在寻找解决方案。一些站点已经弃用了TLSv1.0,而Requests+Openssl(在Windows 7上)很难与此类对等主机建立握手。Wireshark日志显示TLSv1客户端Hello由客户端发出,但主机没有正确应答。此错误随着错误消息请求的显示而传播。即使使用最新的Openssl/pyOpenssl/请求并在Py3.6/2.7.12上进行了尝试,也没有运气。有趣的是,当我将url替换为其他类似“google.com”的url时,日志显示TLSv1.2 Hello是由主机发出和响应的。请检查图片和图片 . 显然,客户机具有TLSv1.2功能,但为什么在前一种情况下使用v1.0 Hello

    [编辑] 我之前的陈述是错的。Wireshark将未完成的TLSv1.2 HELLO交换错误地解释为TLSv1。在深入研究之后,我发现这些主机期望的是纯TLSv1,而不是TLSv1.2的TLSv1回退。与Chrome的日志相比,Openssl在Hello扩展字段(可能是支持的版本)中缺少一些字段。我找到了解决办法。1.强制使用TLSv1协商。2.将默认密码套件更改为py3.4样式以重新启用3DES

    import ssl
    import requests
    from requests.adapters import HTTPAdapter
    from requests.packages.urllib3.poolmanager import PoolManager
    #from urllib3.poolmanager import PoolManager
    from requests.packages.urllib3.util.ssl_ import create_urllib3_context
    
        # py3.4 default
    CIPHERS = (
        'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
        'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
        '!eNULL:!MD5'
    )
    
    class DESAdapter(HTTPAdapter):
        """
        A TransportAdapter that re-enables 3DES support in Requests.
        """
        def create_ssl_context(self):
            #ctx = create_urllib3_context(ciphers=FORCED_CIPHERS)
            ctx = ssl.create_default_context()
            # allow TLS 1.0 and TLS 1.2 and later (disable SSLv3 and SSLv2)
            #ctx.options |= ssl.OP_NO_SSLv2
            #ctx.options |= ssl.OP_NO_SSLv3 
            #ctx.options |= ssl.OP_NO_TLSv1
            ctx.options |= ssl.OP_NO_TLSv1_2
            ctx.options |= ssl.OP_NO_TLSv1_1
            #ctx.options |= ssl.OP_NO_TLSv1_3
            ctx.set_ciphers( CIPHERS )
            #ctx.set_alpn_protocols(['http/1.1', 'spdy/2'])
            return ctx
    
        def init_poolmanager(self, *args, **kwargs):
            context = create_urllib3_context(ciphers=CIPHERS)
            kwargs['ssl_context'] = self.create_ssl_context()
            return super(DESAdapter, self).init_poolmanager(*args, **kwargs)
    
        def proxy_manager_for(self, *args, **kwargs):
            context = create_urllib3_context(ciphers=CIPHERS)
            kwargs['ssl_context'] = self.create_ssl_context()
            return super(DESAdapter, self).proxy_manager_for(*args, **kwargs)
    
    tmoval=10
    proxies={}
    hdr = {'Accept-Language':'zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4', 'Cache-Control':'max-age=0', 'Connection':'keep-alive', 'Proxy-Connection':'keep-alive', #'Cache-Control':'no-cache', 'Connection':'close',
            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36',
            'Accept-Encoding':'gzip,deflate,sdch','Accept':'*/*'}
    ses = requests.session()
    ses.mount(url, DESAdapter())
    
    response = ses.get(url, timeout=tmoval, headers = hdr, proxies=proxies)
    
    [EDIT2] 当您的HTTPS url包含任何大写字母时,修补程序将无法工作。您需要将它们反转为小写。堆栈请求/urllib3/openssl中的未知内容导致修补程序逻辑恢复为默认的TLS1.2方式

    [EDIT3]

    mount调用将传输适配器的特定实例注册到前缀。挂载后,使用该会话发出的、其URL以给定前缀开头的任何HTTP请求都将使用给定的传输适配器

    因此,要使所有HTTPS请求包括服务器随后重定向以使用新适配器的请求,必须将此行更改为:

    ses.mount('https://', DESAdapter())
    

    不知怎的,它解决了上面提到的大写字母问题。

    以前有人遇到过这种问题吗。。。?我是新的爬行网站。我在想,也许这个网站不能被取代?有人能帮我吗?非常感谢。有人试过吗?我真的想知道如何解决这个错误。谢谢。非常感谢您分享您的解决方案。我以为没人能解决这个棘手的问题。你是我项目的灯塔!(已经推迟了一段时间…)感谢卢卡斯对缩进的修正,这是正确的。