Это простой пример без обработки ошибок, переподключение.Надеюсь, я написал правильный ответ;)
import urllib.request as urllib2
http_proxy = {
'user': ''
, 'passwd': ''
, 'server': '67.205.151.211'
, 'port': '3128'
}
# change of IP address
page = urllib2.urlopen("http://httpbin.org/ip").read()
print(page)
# http://username:password@someproxyserver.com:1337
http_proxy_full_auth_string = "http://%s:%s@%s:%s" % (http_proxy["user"],
http_proxy["passwd"],
http_proxy["server"],
http_proxy["port"])
proxy_handler = urllib2.ProxyHandler({"http": http_proxy_full_auth_string,
"https": http_proxy_full_auth_string})
opener = urllib2.build_opener(proxy_handler)
postDatas = {"User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Cache-Control": "no-cache",
"Pragma": "no-cache"}
request = urllib2.Request("http://httpbin.org/ip", None, postDatas)
connection = opener.open(request, timeout=10)
page = connection.read()
# except Exception as err:
# # Si il y a une erreur de connexion (timeout etc.)
# result.add_error(err, "%s ne repond pas" % url)
# else:
connection.close()
print(page)