Requests s.get(url,verify = False) error [Python]

筅森魡賤 提交于 2019-12-11 14:59:59

问题


I'm attempting to write a program that grabs data from my password protected gradebook and analyzes it for me because my university's gradebook doesn't automatically calculate averages. I'm running into issues at the very beginning of my program and it's growing more and more frustrating. I'm running on Python 2.7.9.

This is the Code.

import logging
import requests
import re
url = "https://learn.ou.edu/d2l/m/login"

s = requests.session()
r = s.get(url,verify = False)

This is the error that is occurring.

Traceback (most recent call last):
  File "/Users/jackson/Desktop/untitled folder/Grade Calculator.py", line 7, in <module>
    r = s.get(url,verify = False)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 473, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 431, in send
    raise SSLError(e, request=request)
SSLError: EOF occurred in violation of protocol (_ssl.c:581)

Even weirder, this only happens with the gradebook URL. When I use a different URL such as "http://login.live.com", I get this error.

Warning (from warnings module):
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 734
    InsecureRequestWarning)
InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html

Does anybody know what I could to to fix this issue? Thanks, Jackson.


回答1:


Requests does not support so you need subclass the HTTPAdapter

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1)


import logging
import requests
import re
url = "https://learn.ou.edu/d2l/m/login"
s = requests.session()
s.mount('https://', MyAdapter())
r = s.get(url,verify = False)

print r.status_code

Gives status code:

200

This is answered here



来源:https://stackoverflow.com/questions/27499456/requests-s-geturl-verify-false-error-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!