Using an HTTP PROXY - Python
You can do it even without the HTTP_PROXY environment variable. Try this sample:
import urllib2
proxy_support = urllib2.ProxyHandler({"http":"http://61.233.25.166:80"})
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
html = urllib2.urlopen("http://www.google.com").read()
print html
In your case it really seems that the proxy server is refusing the connection.
Something more to try:
import urllib2
#proxy = "61.233.25.166:80"
proxy = "YOUR_PROXY_GOES_HERE"
proxies = {"http":"http://%s" % proxy}
url = "http://www.google.com/search?q=test"
headers={'User-agent' : 'Mozilla/5.0'}
proxy_support = urllib2.ProxyHandler(proxies)
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler(debuglevel=1))
urllib2.install_opener(opener)
req = urllib2.Request(url, None, headers)
html = urllib2.urlopen(req).read()
print html
Edit 2014:
This seems to be a popular question / answer. However today I would use third party requests
module instead.
For one request just do:
import requests
r = requests.get("http://www.google.com",
proxies={"http": "http://61.233.25.166:80"})
print(r.text)
For multiple requests use Session
object so you do not have to add proxies
parameter in all your requests:
import requests
s = requests.Session()
s.proxies = {"http": "http://61.233.25.166:80"}
r = s.get("http://www.google.com")
print(r.text)
How to apply proxy with authentication using http.client for API
I ended up using the requests package, they make it very simple to channel your request through a proxy, sample code below for reference:
import requests
proxies = {
'http': f"http://username:pwd@webproxy.subdomain.website.com:8080",
'https': f"https://username:pwd@webproxy.subdomain.website.com:8080"
}
url = 'https://api.cognitive.microsoft.com/bing/v7.0/entities/'
# query string parameters
params = 'mkt=' + 'en-US' + '&q=' + urllib.parse.quote (query)
# custom headers
headers = {'Ocp-Apim-Subscription-Key': subscriptionKey}
#start session
session = requests.Session()
#persist proxy across request
session.proxies = proxies
# make GET request
r = session.get(url, params=params, headers=headers)
Creating an HTTPS proxy server in Python
The problem is actually not related to SSL at all but caused by a misunderstanding of how a HTTP proxy for HTTPS works. Such a proxy is not doing SSL at all. It is instead just used to create a tunnel to the final server and the client then creates the HTTPS connection trough this tunnel, keeping the end-to-end encryption this way.
The tunnel itself is created using the HTTP CONNECT method. And this is exactly what you are getting here on your SSL socket:
ssl.SSLError: [SSL: HTTPS_PROXY_REQUEST] https proxy request (_ssl.c:1108)
^^^^^^^^^^^^^^^^^^^
Related Topics
How to Sort Python List of Strings of Numbers
How to Put Parameterized SQL Query into Variable and Then Execute in Python
Efficiently Convert Uneven List of Lists to Minimal Containing Array Padded with Nan
Moving Balls in Tkinter Canvas
How to Make a Character Jump in Pygame
Differencebetween _Init_ and _Call_
How to Reversibly Store and Load a Pandas Dataframe To/From Disk
How to Make Abstract Classes in Python
How to Change the Name of a Django App
Proper Indentation for Multiline Strings
Understanding _Getitem_ Method
Django: Multiple Models in One Template Using Forms
Using a Pre-Trained Word Embedding (Word2Vec or Glove) in Tensorflow
Detect & Record Audio in Python
Looking for Recommendation on How to Convert PDF into Structured Format