Skip to content

Instantly share code, notes, and snippets.

@zeehio
Last active October 24, 2024 07:52
Show Gist options
  • Save zeehio/7a5f780462f49c29a1cc96c429425da1 to your computer and use it in GitHub Desktop.
Save zeehio/7a5f780462f49c29a1cc96c429425da1 to your computer and use it in GitHub Desktop.
Use requests with truststore from a package
# Use requests with truststore from a package
#
# from requests_truststore import requests
# resp = requests.get("https://google.com")
from requests import Session
from requests.adapters import HTTPAdapter
import ssl
import truststore
class TrustStoreHTTPAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
super().init_poolmanager(*args, **kwargs, ssl_context=context)
def _get_request_session():
client_session = Session()
client_session.mount("https://", TrustStoreHTTPAdapter())
return client_session
requests = _get_request_session()
__all__ = ['requests', 'TrustStoreHTTPAdapter']
if __name__ == '__main__':
resp = requests.get("https://google.com")
print(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment