我想允许在运行时切换代理。我的(丑陋的)代码是:
import urllib.request
proxydict={'http':'http://45.63.66.17:8080'}
proxydict2={'http':'http://3.212.39.212:80'}
proxy_support1 = urllib.request.ProxyHandler(proxydict)
opener1 = urllib.request.build_opener(proxy_support1)
proxy_support2 = urllib.request.ProxyHandler(proxydict2)
opener2 = urllib.request.build_opener(proxy_support2)
print(opener1.open('http://icanhazip.com').read())
print(opener2.open('http://icanhazip.com').read())
但是,它不能很好地工作。 (我会检查代理是否在线。)如何在python中完成?是否存在更优雅的方式?