比如有时候python程序中需要下载或者联网点东西,需要代理
1、requests 例子
import requests# 代理服务器的地址和端口
proxy_host = 'your_proxy_server_ip'
proxy_port = 'your_proxy_server_port'# 创建代理字典
proxy = {'http': f'http://{proxy_host}:{proxy_port}','https': f'https://{proxy_host}:{proxy_port}'
}# 发送请求时使用代理
response = requests.get('http://example.com', proxies=proxy)# 打印响应内容
print(response.text)
2、os.environ
os.environ是一个字典,它包含了当前环境的所有环境变量
代理可以使用clash本地全局的
import os
os.environ['http_proxy']='http://127.0.0.1:7890'
os.environ['https _proxy']='http://127.0.0.1:7890'
使用的是需要认证的代理服务器,你需要在代理地址中包含用户名和密码,例如:
os.environ['http_proxy'] = 'http://username:password@127.0.0.1:7890'
os.environ['https_proxy'] = 'http://username:password@127.0.0.1:7890'
确保将username和password替换为你的代理服务器的实际用户名和密码。