standardmäßig urlopen
verwendet die Umgebungsvariable http_proxy
die HTTP-Proxy zu verwenden, fest:
$ export http_proxy='http://myproxy.example.com:1234'
$ python myscript.py # Using http://myproxy.example.com:1234 as a proxy
Wenn Sie stattdessen einen Proxy in Ihrer Anwendung angeben möchten, können Sie eine proxies
ar geben gument zu urlopen
:
proxies = {'http': 'http://myproxy.example.com:1234'}
print "Using HTTP proxy %s" % proxies['http']
urllib.urlopen("http://www.google.com", proxies=proxies)
Edit: Wenn ich Ihre Kommentare richtig verstehe, Sie jeden Proxy mehrere Proxies und drucken wollen versuchen, wie Sie es versuchen. Wie wäre es mit so etwas?
candidate_proxies = ['http://proxy1.example.com:1234',
'http://proxy2.example.com:1234',
'http://proxy3.example.com:1234']
for proxy in candidate_proxies:
print "Trying HTTP proxy %s" % proxy
try:
result = urllib.urlopen("http://www.google.com", proxies={'http': proxy})
print "Got URL using proxy %s" % proxy
break
except:
print "Trying next proxy in 5 seconds"
time.sleep(5)
urllib2 http://StackOverflow.com/Questions/1450132/Proxy-with-URllib2 –