Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right usage of second argument in proxy

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?

like image 372
MITHU Avatar asked Dec 09 '18 07:12

MITHU


1 Answers

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'}
like image 182
t.m.adam Avatar answered Oct 18 '22 20:10

t.m.adam