user4671628
user4671628

Reputation:

Python SOCKS5 proxy client HTTPS

I am trying to make request through a SOCKS5 proxy server over HTTPS but it fails or returns the empty string. I am using PySocks library.

Here is my example

    WEB_SITE_PROXY_CHECK_URL = "whatismyipaddress.com/"
    REQUEST_SCHEMA = "https://"

    host_url = REQUEST_SCHEMA + WEB_SITE_PROXY_CHECK_URL
    socket.connect((host_url, 443))
    request = "GET / HTTP/1.1\nHost: " + host_url + "\nUser-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11\n\n"
    socket.send(request)
    response = socket.recv(4096)
    print response

But it doesn't work, it prints an empty string response.

Is there any way to make HTTPS request through the socks5 proxy in Python ?

Thanks

Upvotes: 1

Views: 10607

Answers (1)

Aakash Verma
Aakash Verma

Reputation: 3994

As of requests version 2.10.0, released on 2016-04-29, requests supports SOCKS.

It requires PySocks, which can be installed with pip install pysocks.

 import requests

 host_url = 'https://example.com'
 #Fill in your own proxies' details
 proxies={http:'socks5://user:pass@host:port',
          https:'socks5://user:pass@host:port'}
 #define headers if you will
 headers={}

 response = requests.get(host_url, headers=headers, proxies=proxies)

Beware, when using a SOCKS proxy, request socks will make HTTP requests with the full URL (e.g., GET example.com HTTP/1.1 rather than GET / HTTP/1.1) and this behavior may cause problems.

Upvotes: 2

Related Questions