DEV Community

Ali Orhun Akkirman for Açıklab

Posted on

2

Python requests üzerinde sertifika kullanımı

Python'ın request kütüphanesinde çalışırken özellikle self-signed sertifikalar ile sorunlar yaşandığında muhtemelen sistem geneline eklediğiniz bir sertifikanın Python request kütüphanesinde kullanılmadığını görebilirsiniz.

Bunun örneği için aşağıdaki gibi bir Python kodu yazarak başlayabiliriz.

import requests

response = requests.get(url="https://en.wikipedia.org/wiki/Zonguldak")
Enter fullscreen mode Exit fullscreen mode

Bu kod sonucu aşağıdaki gibi bir hata alınmaktadır.

requests.exceptions.SSLError: HTTPSConnectionPool(host='en.wikipedia.org', port=443): Max retries exceeded with url: /wiki/Zonguldak (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:997)')))
Enter fullscreen mode Exit fullscreen mode

Bu hatadan kurtulmanın bir yolu aşağıdaki satırda "verify = False" parametresinin eklenmesi olarak internette çözüm bulunabilmektedir. Fakat bu adımla birlikte sertifika kontrolünü kontrol edilmemesini sağlamaktasınız.

response = requests.get(url="https://en.wikipedia.org/wiki/Zonguldak",verify = False)
Enter fullscreen mode Exit fullscreen mode

Bu nedenle Debian tabanlı sistemlerde bu sorunun en güzel çözümü aşağıdaki gibi uygulanabilmektedir. RPM tabanlı sistemlerde ise ca-certificates.crt yerine ca-bundle.crt kullanılmaktadır.

import requests
import os

os.environ['REQUESTS_CA_BUNDLE'] = os.path.join('/etc/ssl/certs/','ca-certificates.crt')

response = requests.get(url="https://en.wikipedia.org/wiki/Zonguldak")
Enter fullscreen mode Exit fullscreen mode

Bu şekilde çalıştırıldığında artık istenilen sunucuya SSL ile bağlantı kurabilirsiniz. Tabi ki sistem genelinde ilgili sertifikanın eklenmiş olması gerekeceğini unutmayalım.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more