Spaces:
Paused
Paused
Download proxy.py from AdeBagusSumantri/Sc: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/AdeBagusSumantri/Sc/resolve/main/proxy.py
- Command line
-
hf download hf://spaces/AdeBagusSumantri/Sc/proxy.py
-
curl -L -o proxy.py https://huggingface.co/spaces/AdeBagusSumantri/Sc/resolve/main/proxy.py
1.06 kB
| import requests | |
| import socket | |
| TOR_PROXY = "socks5h://127.0.0.1:9050" | |
| def get(url, **kwargs): | |
| proxies = {"http": TOR_PROXY, "https": TOR_PROXY} | |
| return requests.get(url, proxies=proxies, **kwargs) | |
| def post(url, **kwargs): | |
| proxies = {"http": TOR_PROXY, "https": TOR_PROXY} | |
| return requests.post(url, proxies=proxies, **kwargs) | |
| def new_identity(): | |
| """Minta IP baru dari Tor""" | |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
| s.connect(('127.0.0.1', 9051)) | |
| s.sendall(b'AUTHENTICATE ""\r\nSIGNAL NEWNYM\r\n') | |
| def patch_all(): | |
| """ | |
| Patch requests.Session secara global — semua requests otomatis lewat Tor. | |
| Tidak patch socket level untuk menghindari segfault. | |
| """ | |
| _original_init = requests.Session.__init__ | |
| def _patched_init(self, *args, **kwargs): | |
| _original_init(self, *args, **kwargs) | |
| self.proxies.update({"http": TOR_PROXY, "https": TOR_PROXY}) | |
| requests.Session.__init__ = _patched_init | |
| print("[proxy] ✅ patch_all() aktif — semua requests lewat Tor") | |