Từ tài liệu:
requests
cũng có thể bỏ qua việc xác minh chứng chỉ SSL nếu bạn đặtverify
thành sai.>>> requests.get['//kennethreitz.com', verify=False]
Nếu bạn đang sử dụng mô-đun của bên thứ ba và muốn vô hiệu hóa séc, thì đây là Trình quản lý ngữ cảnh mà Monkey vá requests
và thay đổi để
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
0 là mặc định và ngăn chặn cảnh báo. 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
Đây là cách bạn sử dụng nó:
with no_ssl_verification[]:
requests.get['//wrong.host.badssl.example/']
print['It works']
requests.get['//wrong.host.badssl.example/', verify=True]
print['Even if you try to force it to']
requests.get['//wrong.host.badssl.example/', verify=False]
print['It resets back']
session = requests.Session[]
session.verify = True
with no_ssl_verification[]:
session.get['//wrong.host.badssl.example/', verify=True]
print['Works even here']
try:
requests.get['//wrong.host.badssl.example/']
except requests.exceptions.SSLError:
print['It breaks']
try:
session.get['//wrong.host.badssl.example/']
except requests.exceptions.SSLError:
print['It breaks here again']
Lưu ý rằng mã này đóng tất cả các bộ điều hợp mở xử lý yêu cầu được vá khi bạn rời khỏi trình quản lý ngữ cảnh. Điều này là do các yêu cầu duy trì nhóm kết nối mỗi lần và xác thực chứng chỉ chỉ xảy ra một lần trên mỗi kết nối nên những điều bất ngờ như thế này sẽ xảy ra:
>>> import requests
>>> session = requests.Session[]
>>> session.get['//wrong.host.badssl.example/', 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: //urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning]
>>> session.get['//wrong.host.badssl.example/', 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: //urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecureRequestWarning]
Giới thiệu
Trong hướng dẫn này, chúng tôi sẽ tìm hiểu cách vô hiệu hóa xác thực SSL bằng thư viện Python yêu cầu.
Xác thực SSL là cực kỳ quan trọng vì lý do bảo mật và nó nên được thực hiện trong các kịch bản ứng dụng thực sự. Tuy nhiên, trong các phát triển, rất phổ biến là chúng tôi muốn gửi các yêu cầu đối với một máy chủ thử nghiệm có thể có một chứng chỉ đã ký. Vì vậy, rất hữu ích để có thể vô hiệu hóa xác thực này cho các trường hợp sử dụng đó. and it should be done in real application scenarios. Nonetheless, during the developments, it is very common that we want to send requests against a testing server that might have a self a signed certificate. Thus, it is useful to be able to disable this validation for such use cases.
Để tránh phải thiết lập một máy chủ thử nghiệm, chúng tôi sẽ sử dụng các ví dụ từ trang web BadSSL. Cụ thể, chúng tôi sẽ thực hiện yêu cầu nhận được điểm cuối này, có chứng chỉ tự ký.
Nếu bạn không có thư viện yêu cầu được cài đặt, nó có thể dễ dàng thực hiện bằng PIP, chỉ bằng cách gửi lệnh sau:
Hướng dẫn này đã được thử nghiệm trên Python 3.7.2 với phiên bản 2.23.0 của thư viện yêu cầu.
Mật mã
Chúng tôi sẽ bắt đầu bằng cách nhập mô -đun yêu cầu.requests module.
Sau đó, để thực hiện yêu cầu, chúng tôi chỉ cần gọi phương thức yêu cầu. Là đầu vào đầu tiên, chúng tôi sẽ vượt qua một chuỗi với phương thức HTTP [Get Get Get] và là đầu vào thứ hai, điểm cuối mà chúng tôi muốn gửi yêu cầu.request method. As first input we will pass a string with the HTTP method [“GET”] and as second input the endpoint to which we want to send the request.
Sau đó, chúng tôi sẽ vượt qua một tham số tùy chọn được gọi là xác minh với giá trị sai. Điều này sẽ cho phép bỏ qua xác thực SSL.verify with the value False. This will allow to skip the SSL validation.
Lưu ý rằng, như đầu ra, phương thức này trả về một đối tượng phản hồi lớp, chúng tôi sẽ lưu trữ trong một biến.
response = requests.request["GET", "//self-signed.badssl.com/", verify = False]
Để hoàn thiện, chúng tôi sẽ in phản hồi từ máy chủ, có thể thu được trong thuộc tính văn bản của đối tượng phản hồi.text property of the Response object.
Mã đầy đủ có thể được nhìn thấy dưới đây.
import requests response = requests.request["GET", "//self-signed.badssl.com/", verify = False] print[response.text]
Để so sánh, chúng tôi cũng sẽ thực hiện cùng một yêu cầu mà không bỏ qua xác thực SSL [khi không được chỉ định, tham số Xác minh được đặt thành TRUE].verify is set to True].
import requests response = requests.request["GET", "//self-signed.badssl.com/"] print[response.text]
Kiểm tra mã
Để kiểm tra mã trước đó, chúng tôi sẽ bắt đầu bằng cách chạy ví dụ nơi chúng tôi giữ xác thực SSL. Tôi sẽ sử dụng Idle, một ide python.
Bạn có thể kiểm tra kết quả trong Hình 1. Như có thể thấy, một ngoại lệ được ném, cho thấy chứng chỉ được tự ký.
Bây giờ chúng tôi sẽ chạy ví dụ nơi chúng tôi vô hiệu hóa xác thực SSL. Kết quả có thể được kiểm tra dưới đây trong Hình 2.
Có thể thấy, bây giờ chúng ta có thể thực hiện yêu cầu và nhận được phản hồi. Tuy nhiên, lưu ý rằng một cảnh báo được gửi đến vỏ chỉ ra rằng xác thực chứng chỉ của Cameron được khuyến khích mạnh mẽ. Điều này giúp nhấn mạnh rằng, trong các kịch bản ứng dụng thực sự, việc xác nhận như vậy nên được thực hiện và do đó, cách tiếp cận chúng ta đang thấy ở đây chỉ nên được tuân theo cho các kịch bản thử nghiệm được kiểm soát.certificate validation is strongly advised“. This helps emphasize that, in real application scenarios, such validation should be performed and thus the approach we are seeing here should only be followed for controlled testing scenarios.