Answer the question
In order to leave comments, you need to log in
Why is the request not going through the proxy(python)?
I recently started learning python, I ran into such a problem: I'm trying to make a request through a proxy server, I read the documentation, I did everything right, it seems, but for some reason it doesn't work.
I check this way: I download the page, write it to a file, see if there is an inscription "in use" opposite the "proxy" item. =)
Tell me, please, what is the mistake
from urllib import request
prox={"http": "http://107.170.106.64:8888"}
request.ProxyHandler(prox)
r=request.urlopen("http://2ip.ru")
f=open("site.html","w",encoding="utf-8")
f.write(r.read().decode("utf-8"))
f.close()
r.close()
Answer the question
In order to leave comments, you need to log in
Here =)
prox={"http": "http://107.170.106.64:8888"}
opener = urllib.request.FancyURLopener(prox)
r = opener.open("http://2ip.ru/")
I'm just learning Python myself, the documentation also has this thing:
Request.set_proxy(host, type)
Prepare the request by connecting to a proxy server. The host and type will replace those of the instance, and the instance’s selector will be the original URL given in the constructor.
ProxyHandler Objects
ProxyHandler.protocol_open(request)
The ProxyHandler will have a method protocol_open() for every protocol which has a proxy in the proxies dictionary given in the constructor. The method will modify requests to go through the proxy, by calling request.set_proxy(), and call the next handler in the chain to actually execute the protocol.
http_proxy = "http://10.10.1.10:3128"
https_proxy = "https://10.10.1.11:1080"
ftp_proxy = "ftp://10.10.1.10:3128"
proxyDict = {
"http" : http_proxy,
"https" : https_proxy,
"ftp" : ftp_proxy
}
r = requests.get(url, headers=headers, proxies=proxyDict)
prox={"http": "http://107.170.106.64:8888"}
hnd = request.ProxyHandler(prox)
opn = request.build_opener(hnd)
request.install_opener(opn)
In python 2.7 it is implemented something like this
import urllib2
prox = urllib2.ProxyHandler({"http":"http://107.170.106.64:8888"})
opener = urllib2.build_opener(prox)
urllib2.install_opener(opener)
html = urllib2.urlopen("http://2ip.ru").read()
with open("site.html","w",encoding="utf-8") as f:
f.write(html)
f.write(requests.get("http://2ip.ru", proxies={"http": "http://107.170.106.64:8888"}).text)
respectively
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question