Last active
April 3, 2025 15:34
-
-
Save Inndy/79efe5961c56e86d4ff21dc727d35035 to your computer and use it in GitHub Desktop.
monkey patch socket module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
proxy = os.getenv('socks5_proxy') | |
if proxy: | |
host, port = proxy.split(':') | |
import socks # pip install pysocks | |
import socket | |
def create_connection(address, timeout=None, source_address=None): | |
sock = socks.socksocket() | |
sock.connect(address) | |
return sock | |
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, host, int(port, 10)) | |
socket.socket = socks.socksocket | |
socket.create_connection = create_connection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment