Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test an HTTPS proxy in python

I manage a lot of HTTPS proxys (That's proxies which have an SSL connection of their own). I'm building a diagnostic tool in python that attempts to connect to a page through each proxy and email me if it can't connect through one of them.

The way I've set out to go about this is to use urllib to connect through each proxy and return a page which should say "success" with the code below.

def fetch(url):
    connection = urllib.urlopen(
    url,
    proxies={'http':"https://"+server+':443'}
    )
    return connection.read()


print fetch(testURL)

This fetches the page I want perfectly the problem is it will still fetch the page I want even if the proxy server information is incorrect or the proxy server is inactive. So either it never uses the proxy server or it tries it and connects without it when it fails.

How can I correct this?

Edit: No one seems to know how to do this. I'm going to start reading through other languages libraries to see if they can handle it better. Does anyone know if it's easier in another language like Go?

Edit: I just wrote this in a comment below but I think it might be a misunderstanding going around. "The proxy has it's own ssl connection. So if I go to google.com, I first do a key exchange with foo.com and then another with the destination address bar.com or the destination address baz.com The destination doesn't have to be https, the proxy is https"

like image 917
Josh Horowitz Avatar asked Sep 04 '14 02:09

Josh Horowitz


People also ask

How do I check if a proxy is working in Python?

A simple python script to check if a proxy is working. Simply put proxy:port in array. If you want to check if internet is working or not, leave the array empty.

How do I run a python proxy?

To use a proxy in Python, first import the requests package. Next create a proxies dictionary that defines the HTTP and HTTPS connections. This variable should be a dictionary that maps a protocol to the proxy URL. Additionally, make a url variable set to the webpage you're scraping from.

Can you use proxy for HTTPS?

Modern proxy servers can be used as gateways for requests that access both HTTP and HTTPS resources.


2 Answers

Most people understand https proxy as proxy that understands CONNECT request. My example creates direct ssl connection.

try:
    import http.client as httplib # for python 3.2+
except ImportError:
    import httplib # for python 2.7


con = httplib.HTTPSConnection('proxy', 443) # create proxy connection
# download http://example.com/ through proxy
con.putrequest('GET', 'http://example.com/', skip_host=True)
con.putheader('Host', 'example.com')
con.endheaders()
res = con.getresponse()
print(res.read())

If your proxy is reverse proxy then change

con.putrequest('GET', 'http://example.com/', skip_host=True)

to

con.putrequest('GET', '/', skip_host=True)`
like image 150
Slava Bacherikov Avatar answered Sep 26 '22 18:09

Slava Bacherikov


I assume its not working for https requests. Is this correct? If yes then the above code defines a proxy for only http. Try adding it for https:

proxies={'https':"https://"+server+':443'}

Another option is to use the requests python module instead of urllib. Have a look at http://docs.python-requests.org/en/latest/user/advanced/#proxies

like image 31
Yuvika Avatar answered Sep 23 '22 18:09

Yuvika