M
M
MK2015-01-29 13:04:07
Python
MK, 2015-01-29 13:04:07

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()

PS version 3.4

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
MK, 2015-01-29
@Maxsior

Here =)

prox={"http": "http://107.170.106.64:8888"}
opener = urllib.request.FancyURLopener(prox)
r = opener.open("http://2ip.ru/")

V
Vadim Yakovlev, 2015-01-29
@1nn0

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.

And here it is in the same documentation:
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.

Or here is a construction taken from StackOverflow (using the requests module, not urllib)
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)

Or something like this for your code:
prox={"http": "http://107.170.106.64:8888"}
hnd = request.ProxyHandler(prox)
opn = request.build_opener(hnd)
request.install_opener(opn)

R
Roman Korolev, 2015-01-29
@tenoclock

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)

there is also a requests module that supports proxies.
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 question

Ask a Question

731 491 924 answers to any question