8

What is the right way to use htt(p|ps) in second argument within proxy when I make a requests to a https site? The proxy I used below is just a placeholder.

When I try like this (it works):

proxies = {
  'https': 'http://79.170.192.143:34394',
}

when I try like this (it works as well):

proxies = {
  'https': 'https://79.170.192.143:34394',
}

Is the second htt(p|ps) in proxy just a placeholder and What if I make a requests to a http site?

MITHU
  • 113
  • 3
  • 12
  • 41

1 Answers1

4

In this case both settings are valid. Personally, I'd rather use the appropriate protocol because it is not ambiguous, but both settings will result in the same connection. Even if the protocol is not set,

proxies = {'https': '79.170.192.143:34394'}

all HTTPS requests will use this proxy.

With HTTP and HTTPS proxies it is not required to include the protocol (it can be determined by the request URL scheme). With SOCKS proxies however we must use the correct protocol, eg:

proxies = {'https': 'socks5://79.170.192.143:34394'}

otherwise the connection will fail.

The 'https' key in the proxies dictionary specifies the protocol for the connection to the destination host. If there is a 'https' key, all HTTPS connections will use a proxy. HTTP requests will not be proxied unless there is a 'http' key in proxies.

The 'https' in the proxy URL string specifies the protocol for the communication with the proxy server. Generally, it is important to use the correct protocol (ie https://79.170.192.143:34394) but in this case it makes no difference. Only the proxy host and port are used when creating a HTTPS connection (source code) and the protocol is ignored.

While this specific configuration works, it's best to use the correct protocol. For example, the following proxy settings would fail to to establish a connection.

proxies = {'http': 'https://79.170.192.143:34394'}
proxies = {'https': 'socks5://79.170.192.143:34394'}
proxies = {'https': 'invalid-scheme://79.170.192.143:34394'}
t.m.adam
  • 15,106
  • 3
  • 32
  • 52
  • Thanks a lot @ t.m.adam. I knew you would surely lead me to the solution. – MITHU Aug 23 '19 at 15:50
  • Look at the answers that I've got in my bounty question @t.m.adam. Your solution is always elegant and that is the reason I carefully disturb you every now and then. Thanks. – robots.txt Nov 14 '19 at 06:30