python设置全局socks5代理
-
安装pysocks
pip install pysocks
-
实现方法
import socks
import socket
def up_global_proxy(host: str, port: int, username: str=None, password :str=None):
"""
开启全局代理
:return:
"""
socks.setdefaultproxy(socks.SOCKS5, host, port, username=username, password=password)
socket.socket = socks.socksocket
print(f"全局代理设置成功,当前代理为:{host}:{port}")
def off_global_proxy():
"""
关闭全局代理
:return:
"""
socks.setdefaultproxy()
print("全局代理已关闭")