Từ the documentation:
Các yêu cầu cũng có thể bỏ qua xác minh certficate SSL nếu bạn thiết lập verify
False.
>>> requests.get('https://kennethreitz.com', verify=False)
<Response [200]>
Nếu bạn sử dụng một mô-đun của bên thứ ba và muốn vô hiệu hóa kiểm tra, đây là một người quản lý bối cảnh đó các bản vá lỗi khỉ requests
và thay đổi nó để verify=False
là mặc định và ngăn chặn các cảnh báo.
import warnings
import requests
import contextlib
try:
from functools import partialmethod
except ImportError:
# Python 2 fallback: https://gist.github.com/carymrobbins/8940382
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance, *(self.args or()), **(self.keywords or {}))
@contextlib.contextmanager
def no_ssl_verification():
old_request = requests.Session.request
requests.Session.request = partialmethod(old_request, verify=False)
warnings.filterwarnings('ignore', 'Unverified HTTPS request')
yield
warnings.resetwarnings()
requests.Session.request = old_request
Và một ví dụ về cách sử dụng nó:
with no_ssl_verification():
requests.get('https://93.184.216.3/')
print('It works')
try:
requests.get('https://93.184.216.3/')
except requests.exceptions.SSLError:
print('It broke')
Cảm ơn, làm việc này nếu bạn có vài yêu cầu gọi bên trong mã của riêng bạn, nhưng hãy tưởng tượng rằng tôi muốn vô hiệu hóa điều này trong thư viện phần ba thứ ba có sử dụng các yêu cầu, ... sẽ không thể sửa được lib của bên thứ 3 như thế này. – sorin
@sorin: Chỉ cần vá lỗi 'yêu cầu' và có' verify' mặc định thành 'False'. – Blender
Làm cách nào để ngăn chặn thông điệp cảnh báo khó chịu mà vẫn được in? – Michael