Learn Steps

How to download under proxy with python

Proxies are headache for many people. They restrict you from many things you love to do. In this short article I will tell you how to write scripts to work under proxy in python. Its really simple and you don’t need to know anything exceptional except a bit of python programming language. Lets see how to download under proxy with python

Simple diagram to show what is proxy. Taken from https://www.drupal.org/files/project-images/proxy.png

How to download under proxy with python

For download, in python people generally use two libraries. First one is requests and the other one is urllib.

We will discuss the script in both the libraries.

urllib

import urllib
proxies = {'http': 'http://YOUR_USERNAME:YOURPASSWORD@192.168.1.107:3128',
           'https': 'https://YOUR_USRNAME:YOURPASSOWRD@192.168.1.107:3128'}
res=urllib.urlopen("http://www.py4inf.com/cover.jpg",proxies=proxies)
con=res.read()
outf=open("imgname.jpg",'wb')
outf.write(con)
outf.close()

What we did here:

We import the library, set the proxies in dictionary, open the url with proxies as parameters. Read the content of the url then write it down in a file. Here we saved an image and it is done in no time. Really simple isn’t it.

Requests

import requests
proxies = {'http': 'http://YOUR_USERNAME:YOURPASSWORD@192.168.1.107:3128',
           'https': 'https://YOUR_USRNAME:YOURPASSOWRD@192.168.1.107:3128'}
res=requests.get("http://www.py4inf.com/cover.jpg",proxies=proxies)
con=res.content
outf=open("imgname.jpg",'wb')
outf.write(con)
outf.close()

Yes exactly the same there is a very little change which anyone can understand so I don’t need to explain it.

So this is the end of article How to download under proxy with python.