Python Glossary Русский

all Python 3urllib.requestterm

urllib.request.ProxyHandler

cookies and proxies

Routes requests through a proxy server; a dict sets proxies per scheme, and without arguments they come from the environment.

Syntax

urllib.request.ProxyHandler(proxies=None)

Without arguments proxies come from http_proxy/https_proxy, and on Windows and macOS also from system settings, so the code behaves differently on different machines. ProxyHandler({}) turns proxies off completely. HTTPS needs its own 'https' key — the 'http' key does not apply to it. Hosts listed in no_proxy are reached directly.

Examples

import urllib.request
handler = urllib.request.ProxyHandler({'http': 'http://proxy.local:3128'})
req = urllib.request.Request('http://example.com/page')
handler.http_open(req)  # только перенаправляет запрос, в сеть не ходит
print(req.host, req.selector)  # → proxy.local:3128 http://example.com/page

See also

Open in the full glossarydocs.python.orgReport an inaccuracy