Skip to content

Instantly share code, notes, and snippets.

@selcukcihan
Created March 29, 2023 07:58
Show Gist options
  • Save selcukcihan/ec5d225464d517ca637e6188b8c75a61 to your computer and use it in GitHub Desktop.
Save selcukcihan/ec5d225464d517ca637e6188b8c75a61 to your computer and use it in GitHub Desktop.
import http.client
original_request = http.client.HTTPConnection.request
intercepted_requests = []
def intercepted_request(self, *args, **kwargs):
intercepted_requests.append(args[1][1:])
return original_request(self, *args, **kwargs)
# intercept requests
http.client.HTTPConnection.request = intercepted_request
def test_urllib3():
import urllib3
_http = urllib3.PoolManager()
_http.request("GET", "https://www.example.org/urllib3")
def test_requests():
import requests
requests.get("https://www.example.org/requests")
def test_httpx():
import httpx
httpx.get('https://www.example.org/httpx')
def test_httpx_async():
import asyncio
import httpx
async def _test():
async with httpx.AsyncClient() as client:
await client.get('https://www.example.com/httpx_async')
asyncio.run(_test())
def test_aiohttp():
import asyncio
import aiohttp
async def main():
async with aiohttp.ClientSession() as session:
async with session.get('http://httpbin.org/get') as resp:
pass
asyncio.run(main())
test_urllib3()
test_requests()
test_httpx()
test_httpx_async()
test_aiohttp()
print(intercepted_requests)
# revert the interceptor
http.client.HTTPConnection.request = original_request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment