파이썬 요청에서 보안 인증서 확인을 비활성화하는 방법
나는 사용하고있다
import requests
requests.post(url='https://foo.com', data={'bar':'baz'})
하지만 request.exceptions.SSLError가 발생합니다. 웹 사이트의 인증서가 만료되었지만 중요한 데이터를 보내지 않으므로 중요하지 않습니다. 사용할 수있는 'verifiy = False'와 같은 인수가 있다고 생각하지만 찾을 수없는 것 같습니다.
에서 문서 :
verify
False로 설정하면 요청에서 SSL 인증 확인을 무시할 수도 있습니다 .>>> requests.get('https://kennethreitz.com', verify=False) <Response [200]>
타사 모듈을 사용하고 검사를 비활성화하려는 경우 원숭이가 패치 requests
하고 변경 verify=False
하여 기본 설정으로 변경 하고 경고를 표시 하지 않는 컨텍스트 관리자가 있습니다.
import warnings
import contextlib
import requests
from urllib3.exceptions import InsecureRequestWarning
old_merge_environment_settings = requests.Session.merge_environment_settings
@contextlib.contextmanager
def no_ssl_verification():
opened_adapters = set()
def merge_environment_settings(self, url, proxies, stream, verify, cert):
# Verification happens only once per connection so we need to close
# all the opened adapters once we're done. Otherwise, the effects of
# verify=False persist beyond the end of this context manager.
opened_adapters.add(self.get_adapter(url))
settings = old_merge_environment_settings(self, url, proxies, stream, verify, cert)
settings['verify'] = False
return settings
requests.Session.merge_environment_settings = merge_environment_settings
try:
with warnings.catch_warnings():
warnings.simplefilter('ignore', InsecureRequestWarning)
yield
finally:
requests.Session.merge_environment_settings = old_merge_environment_settings
for adapter in opened_adapters:
try:
adapter.close()
except:
pass
사용 방법은 다음과 같습니다.
with no_ssl_verification():
requests.get('https://wrong.host.badssl.com/')
print('It works')
requests.get('https://wrong.host.badssl.com/', verify=True)
print('Even if you try to force it to')
requests.get('https://wrong.host.badssl.com/', verify=False)
print('It resets back')
session = requests.Session()
session.verify = True
with no_ssl_verification():
session.get('https://wrong.host.badssl.com/', verify=True)
print('Works even here')
try:
requests.get('https://wrong.host.badssl.com/')
except requests.exceptions.SSLError:
print('It breaks')
try:
session.get('https://wrong.host.badssl.com/')
except requests.exceptions.SSLError:
print('It breaks here again')
Note that this code closes all open adapters that handled a patched request once you leave the context manager. This is because requests maintains a per-session connection pool and certificate validation happens only once per connection so unexpected things like this will happen:
>>> import requests
>>> session = requests.Session()
>>> session.get('https://wrong.host.badssl.com/', verify=False)
/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
<Response [200]>
>>> session.get('https://wrong.host.badssl.com/', verify=True)
/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning)
<Response [200]>
Use requests.packages.urllib3.disable_warnings()
and verify=False
on requests
methods.
import requests
from urllib3.exceptions import InsecureRequestWarning
# Suppress only the single warning from urllib3 needed.
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# Set `verify=False` on `requests.post`.
requests.post(url='https://example.com', data={'bar':'baz'}, verify=False)
To add to Blender's answer, you can disable SSL for all requests using Session.verify = False
import requests
session = requests.Session()
session.verify = False
session.post(url='https://foo.com', data={'bar':'baz'})
Note that urllib3
, (which Requests uses), strongly discourages making unverified HTTPS requests and will raise an InsecureRequestWarning
.
If you want to send exactly post request with verify=False option, fastest way is to use this code:
import requests
requests.api.request('post', url, data={'bar':'baz'}, json=None, verify=False)
'IT' 카테고리의 다른 글
Android는 .apk 파일을 유지합니까? (0) | 2020.05.21 |
---|---|
pycharm에서 모듈을 어떻게 가져 옵니까? (0) | 2020.05.21 |
ACTION_SEND 의도에 따라 특정 앱을 필터링하고 각 앱마다 다른 텍스트를 설정하는 방법 (0) | 2020.05.21 |
차이점은 무엇입니까 (0) | 2020.05.21 |
데이터베이스의 모든 사용자에 대한 모든 권한 / 액세스를 찾는 SQL Server 쿼리 (0) | 2020.05.21 |