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 Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay