python requests的代理检测
def getproxies_environment():
proxies = {}
for name, value in os.environ.items():
name = name.lower()
if value and name[-6:] == '_proxy':
proxies[name[:-6]] = value
if 'REQUEST_METHOD' in os.environ:
proxies.pop('http', None)
for name, value in os.environ.items():
if name[-6:] == '_proxy':
name = name.lower()
if value:
proxies[name[:-6]] = value
else:
proxies.pop(name[:-6], None)
return proxies
def getproxies_registry(self):
proxies = {}
try:
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()
except (OSError, ValueError, TypeError):
pass
return proxies
PAC代理检测
需要用到一个第三方模块儿pypac,需要传入一个url,根据pac文件判断
该地址是否需要代理
pac文件内容:
def get_pac_proxy(server_url):
from pypac import get_pac
try:
reg_path = r'Software\Microsoft\Windows\CurrentVersion\Internet Settings'
ie_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, reg_path, 0, (winreg.KEY_WOW64_64KEY + winreg.KEY_READ))
pac_url, _ = winreg.QueryValueEx(ie_key, "AutoConfigURL")
pac = get_pac(url=pac_url)
res = urlparse(server_url)
proxy_address = pac.find_proxy_for_url(server_url, res.netloc)
proxys = proxy_address.split(";")
if proxys and len(proxys) > 0:
proxy_address = proxys[0]
proxy_address = proxy_address.replace("PROXY", "", -1)
proxy_address = proxy_address.replace(";", "", -1)
proxy_address = proxy_address.strip()
items = proxy_address.split(":")
if len(items) == 2:
return items[0], items[1], "HTTP"
except Exception as e:
# traceback.print_exc()
pass
return "", "", ""
代理判断
- 可以通过powershell设置临时代理
set http_proxy=http://127.0.0.1:1080 set https_proxy=http://127.0.0.1:1080
- PAC代理
- 全局代理
注册表中的信息也是有区别的:
Win+R 输入regedit
PAC代理:
全局代理:
以上针对加速器也是可以检测出来的