Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import asyncio
- import logging
- import os
- import ssl
- import sys
- import time
- import aiohttp
- PROXY_PORT = 1443
- PROXY_HOSTNAME = "https://proxy.company.com"
- EXTERNAL_ENDPOINT = "https://www.google.com"
- CUSTOM_CA_BUNDLE = "/var/certs/ca.pem"
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.INFO)
- logger.addHandler(logging.StreamHandler(sys.stdout))
- async def run_example():
- cert = os.environ["TLS_CL_CERT_PATH"]
- key = os.environ["TLS_CL_KEY_PATH"]
- if not thrift_cert or not thrift_key:
- raise ValueError("Missing key TLS cert settings.")
- # For the example lets ensure HTTPS_PROXY is set
- os.environ["HTTPS_PROXY"] = f"{PROXY_HOSTNAME}:{PROXY_PORT}"
- # Setup SSL Fun
- ssl_ctx = ssl.create_default_context(cafile=CA_BUNDLE)
- ssl_ctx.load_cert_chain(cert, key)
- conn = aiohttp.TCPConnector(ssl=ssl_ctx)
- start_time = time.time()
- # trust_env allows HTTP(s)_PROXY vars to work
- async with aiohttp.ClientSession(connector=conn, trust_env=True) as session:
- async with session.get(EXTERNAL_ENDPOINT) as response:
- logger.info(
- "Received response with status code "
- + f"{response.status} in {time.time() - start_time}s"
- )
- if __name__ == "__main__":
- asyncio.run(run_example())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement