MCPcopy Index your code
hub / github.com/python-websockets/websockets / get_proxy

Function get_proxy

src/websockets/proxy.py:98–131  ·  view source on GitHub ↗

Return the proxy to use for connecting to the given WebSocket URI, if any.

(uri: WebSocketURI)

Source from the content-addressed store, hash-verified

96
97
98def get_proxy(uri: WebSocketURI) -> str | None:
99 """
100 Return the proxy to use for connecting to the given WebSocket URI, if any.
101
102 """
103 if urllib.request.proxy_bypass(f"{uri.host}:{uri.port}"):
104 return None
105
106 # According to the _Proxy Usage_ section of RFC 6455, use a SOCKS5 proxy if
107 # available, else favor the proxy for HTTPS connections over the proxy for
108 # HTTP connections.
109
110 # The priority of a proxy for WebSocket connections is unspecified. We give
111 # it the highest priority. This makes it easy to configure a specific proxy
112 # for websockets.
113
114 # getproxies() may return SOCKS proxies as {"socks": "http://host:port"} or
115 # as {"https": "socks5h://host:port"} depending on whether they're declared
116 # in the operating system or in environment variables.
117
118 proxies = urllib.request.getproxies()
119 if uri.secure:
120 schemes = ["wss", "socks", "https"]
121 else:
122 schemes = ["ws", "socks", "https", "http"]
123
124 for scheme in schemes:
125 proxy = proxies.get(scheme)
126 if proxy is not None:
127 if scheme == "socks" and proxy.startswith("http://"):
128 proxy = "socks5h://" + proxy[7:]
129 return proxy
130 else:
131 return None
132
133
134def prepare_connect_request(

Callers 4

create_connectionMethod · 0.85
connectFunction · 0.85
test_get_proxyMethod · 0.85

Calls 1

getMethod · 0.45

Tested by 2

test_get_proxyMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…