How to make http requests in python using proxies ?


There are a couple of ways to make HTTP/HTTPS requests in requests. First of all, it deserves keeping in mind that in httplib, you have to spend a great deal of time determining exactly how you would handle TLS or SSL decryption as well as the trust for HTTPS endpoints.

Requests is a Python library (Read here for the basic understanding of Python), It will permit you to send HTTP/1.1 requests using Python. With it, you can include content like headers, form data, multipart files, and parameters via simple Python libraries. It likewise permits you to access the feedback information of Python in the same way.

Install requests:

If you have pip installed on your system, you can install it by running the command-

pip install requests # for python2
pip3 install requests # for python3

Making a GET request:

>>> import requests
>>> response = requests.get('')
>>> print(response)
<Response [200]>
>>> print(response.status_code)
>>> print(response.text)
<!doctype html><html itemscope="" itemtype="" lang="en-IN"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

As the variable's name pointing out, response is response object containing some useful information response.status_code contains the status code and response.text contains the source code of request making above.

The output for above code is-

Parameters can also pass using a dictionary. For example-

>>> params = {
... 'q': 'apple',
... 'client': 'ubuntu',
... }
>>> r = requests.get('', params=params)
>>> print(r.url)

Same like GET using requests module POST, PUT, DELETE, HEAD and OPTIONS requests can also be made. For Detailed documentation visit here. That's all about getting started with requests module. Now coming back to the proxy section we write a script which makes the request using original proxy and proxy through which we want to make the request.

Making a GET request with the proxy-

For proxies without username and password - 

proxies = {
    "http" : 'http://ip:port',
    "https" : 'https://ip:port',

For proxies with username and password - 

proxies = {
    "http" : 'http://username:[email protected]:port',
    "https" : 'https://username:[email protected]:port',

below is the example-

import requests

url = ''

proxies = {
    "http" : '',
    "https" : '',

req = requests.get(url)
print('original proxy is: ', req.text)
request = requests.get(url, proxies=proxies)
print('using proxy is: ', request.text)

you will see the output-

('original proxy is: ', u'{"ip":""}')
('using proxy is: ', u'{"ip":""}')

For testing, you can found free proxies here. That’s all we’ve got to say If you have any query post in comments. Happy scraping :)


Join us for latest updates and blog posts

Recent Posts







RSS / Atom