Skip to content

Instantly share code, notes, and snippets.

@miodeqqq
Created December 8, 2016 20:22
Show Gist options
  • Save miodeqqq/ecf537d66041cfa23d1b3ea1b9a5c737 to your computer and use it in GitHub Desktop.
Save miodeqqq/ecf537d66041cfa23d1b3ea1b9a5c737 to your computer and use it in GitHub Desktop.
Python Request (urllib2) with proxies.
#! /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