Ken T
Ken T

Reputation: 2553

Does Python requests really connect to http proxy?

When I am using the code below with an obviously wrong http proxy, the requests module can still get from the url. How could it be possible? Does it mean that requests won't use http proxy? If so, is there any chance that it won't https proxy as well? I need to confirm that my post and get are done via proxies.

import requests
url=r'https://stackoverflow.com/questions'
proxies={'http':'http://asdasdasd:80'}


with requests.session() as s:
    resp = s.get(url, proxies=proxies)
    print resp
    print resp.text

Upvotes: 3

Views: 5620

Answers (1)

Amber
Amber

Reputation: 526573

You're setting an http proxy but making an https request. Set an https proxy instead (or in addition).

import requests

proxies = {
  'http': 'http://10.10.1.10:3128',
  'https': 'http://10.10.1.10:1080',
}

requests.get('https://example.org', proxies=proxies)

http://docs.python-requests.org/en/master/user/advanced/?highlight=proxies#proxies

Example run with correct type of proxy set:

>>> import requests
>>> requests.get('https://example.org', proxies={'https': 'http://asdasdasd:80'})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 437, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='example.org', port=443):
Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.',
NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at
0x7f3b880b9410>: Failed to establish a new connection: [Errno -2] Name or service not
known',)))
>>>

Upvotes: 5

Related Questions