感谢大家的回复,晚上回家翻了下源码,这里我自问自答下:
关于第二个问题:为什么 Windows 开了 Clash 的系统代理,使用 requests 如果不显示的设置 proxies 这个参数(无论是方法传参,还是设置环境变量)就无法请求的问题。
requests 的 sessions 模块的 merge_environment_settings() 方法调用了 Python 自带的 urllib 库中的 request 模块的 getproxies() 方法。
似乎顺序是这样的:方法传参 > 环境变量 > 注册表
如果方法没传参,环境变量也没有设置 http/https_proxy 的话,代码走到以下 elif 块中,去读 Windows 的注册表:
```
elif
os.name == 'nt':
def getproxies_registry():
"""Return a dictionary of scheme -> proxy server URL mappings.
Win32 uses the registry to store proxies.
"""
# 省略部分代码
try:
# 查询 win 注册表
internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
proxyEnable = winreg.QueryValueEx(internetSettings,
'ProxyEnable')[0]
if proxyEnable:
# Returned as Unicode but problems if not converted to ASCII
proxyServer = str(winreg.QueryValueEx(internetSettings,
'ProxyServer')[0])
if '=' in proxyServer:
# Per-protocol settings
for p in proxyServer.split(';'):
protocol, address = p.split('=', 1)
# See if address has a type:// prefix
if not re.match('^([^/:]+)://', address):
address = '%s://%s' % (protocol, address)
proxies[protocol] = address
else:
# Use one setting for all protocols
if proxyServer[:5] == 'http:':
proxies['http'] = proxyServer
else:
proxies['http'] = 'http://%s' % proxyServer
proxies['https'] = 'https://%s' % proxyServer
proxies['ftp'] = 'ftp://%s' % proxyServer
internetSettings.Close()
return proxies
```