Python Network Programming Resources

Selected Reading

Python Network - Proxy Server



Proxy servers are used to browse to some website through another server so that the browsing remains anonymous. It can also be used to bypass the blocking of specific IP addresses.

We use the requst method from the urllib3 module to access the website by passing the proxy server address as a parameter.

Example

In the below example we use a proxy address to access the website twitter.con for anonymous access. The response status of OK proves the successful access through a proxy server.

main.py

import urllib3
URL = 'https://www.twitter.com'
PROXY_ADDRESS = 'http://265.24.11.6:8080/' 
http = urllib3.ProxyManager(PROXY_ADDRESS)

if __name__ == '__main__':
    resp = http.request('GET', URL)
print("Proxy server returns response headers: %s " %resp.headers)

Output

When we run the above program, we get the following output −

Proxy server returns response headers: cache-control: no-cache, 
no-store, must-revalidate, pre-check=0, post-check=0
content-length: 145960
content-type: text/html;charset=utf-8
date: Mon, 02 Jul 2018 02:06:19 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Mon, 02 Jul 2018 02:06:19 GMT
pragma: no-cache
server: tsa_n
set-cookie: fm=0; Expires=Mon, 02 Jul 2018 02:06:10 GMT; Path=/; Domain=.twitter.com; Secure; HTTPOnly
set-cookie: _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc
2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoPY3JlYXRlZF9hdGwrC
AifvVhkAToMY3NyZl9p%250AZCIlNzlhY2ZhMzdmNGFkYTU0ZTRmMDkxO
DRhNWNiYzI0MDI6B2lkIiUyZTgy%250AOTAyYjY4NTBkMzE3YzNjYTQwN
zZjZDhhYjZhMQ%253D%253D--6807256d74a01129a7b0dcf383f56f16
9fb8a66c; Path=/; Domain=.twitter.com; Secure; HTTPOnly
set-cookie: personalization_id="v1_iDacJdPWUx3e81f8UErWTA=="; 
Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com
set-cookie: guest_id=v1%3A153049717939764273; 
Expires=Wed, 01 Jul 2020 02:06:19 GMT; Path=/; Domain=.twitter.com
set-cookie: ct0=50c8b59b09322825cd750ea082e43bdc; 
Expires=Mon, 02 Jul 2018 08:06:19 GMT; Path=/; Domain=.twitter.com; Secure
status: 200 OK
strict-transport-security: max-age=631138519
x-connection-hash: 89dfbab81abc35dd8f618509c6171bd3
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 312
x-transaction: 007b998000f86eaf
x-twitter-response-tags: BouncerCompliant
x-ua-compatible: IE=edge,chrome=1
x-xss-protection: 1; mode=block; report=https://twitter.com/i/xss_report
Advertisements