Created
December 8, 2016 20:22
-
-
Save miodeqqq/ecf537d66041cfa23d1b3ea1b9a5c737 to your computer and use it in GitHub Desktop.
Python Request (urllib2) with proxies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| from urllib2 import Request, URLError, urlopen, build_opener, ProxyHandler, install_opener | |
| def request_with_proxy(): | |
| proxy = ProxyHandler( | |
| { | |
| 'http': 'localhost:1111', | |
| 'https': 'localhost:1111', | |
| } | |
| ) | |
| opener = build_opener(proxy) | |
| install_opener(opener) | |
| my_headers = { | |
| 'User-Agent': 'whatever', | |
| 'Connection': 'close', | |
| } | |
| my_url = 'http://www.google.com/' | |
| try: | |
| request = Request( | |
| url=my_url, | |
| headers=my_headers, | |
| ) | |
| urlopen(request, timeout=600) | |
| # do some stuff | |
| except URLError as e: | |
| print("Error --> {}".format(e)) | |
| request_with_proxy() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment