답변:
에서 문서 :
requests
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')
이 코드는 컨텍스트 관리자를 떠나면 패치 된 요청을 처리 한 모든 열린 어댑터를 닫습니다. 이는 요청이 세션 당 연결 풀을 유지하고 인증서 유효성 검사는 연결 당 한 번만 발생하므로 예기치 않은 상황이 발생하기 때문입니다.
>>> 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]>
requests
이며 verify
기본값은 False
입니다.
requests.packages.urllib3.disable_warnings()
from urllib3.exceptions import InsecureRequestWarning
다음requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
사용 requests.packages.urllib3.disable_warnings()
및 verify=False
에 requests
방법.
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)
verify=False
어쨌든 참석해야합니다. Tnx.
from urllib3.exceptions import InsecureRequestWarning
다음requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
. 이것은에서 urllib3.exceptions.InsecureRequestWarning
사용하는 정확한 것을 보장하기 때문에 작동합니다 requests
.
환경 변수에서도 수행 할 수 있습니다.
export CURL_CA_BUNDLE=""
export REQUESTS_CA_BUNDLE='your-ca.pem'
os.environ['REQUESTS_CA_BUNDLE'] = 'FiddlerRootCertificate_Base64_Encoded_X.509.cer.pem' # your-ca.pem
verify = False 옵션을 사용하여 정확하게 사후 요청을 보내려면이 코드를 사용하는 것이 가장 빠릅니다.
import requests
requests.api.request('post', url, data={'bar':'baz'}, json=None, verify=False)