MCPcopy
hub / github.com/urllib3/urllib3 / _new_conn

Method _new_conn

src/urllib3/contrib/socks.py:98–154  ·  view source on GitHub ↗

Establish a new connection via the SOCKS proxy.

(self)

Source from the content-addressed store, hash-verified

96 super().__init__(*args, **kwargs)
97
98 def _new_conn(self) -> socks.socksocket:
99 """
100 Establish a new connection via the SOCKS proxy.
101 """
102 extra_kw: dict[str, typing.Any] = {}
103 if self.source_address:
104 extra_kw["source_address"] = self.source_address
105
106 if self.socket_options:
107 extra_kw["socket_options"] = self.socket_options
108
109 try:
110 conn = socks.create_connection(
111 (self.host, self.port),
112 proxy_type=self._socks_options["socks_version"],
113 proxy_addr=self._socks_options["proxy_host"],
114 proxy_port=self._socks_options["proxy_port"],
115 proxy_username=self._socks_options["username"],
116 proxy_password=self._socks_options["password"],
117 proxy_rdns=self._socks_options["rdns"],
118 timeout=self.timeout,
119 **extra_kw,
120 )
121
122 except SocketTimeout as e:
123 raise ConnectTimeoutError(
124 self,
125 f"Connection to {self.host} timed out. (connect timeout={self.timeout})",
126 ) from e
127
128 except socks.ProxyError as e:
129 # This is fragile as hell, but it seems to be the only way to raise
130 # useful errors here.
131 if e.socket_err:
132 error = e.socket_err
133 if isinstance(error, SocketTimeout):
134 raise ConnectTimeoutError(
135 self,
136 f"Connection to {self.host} timed out. (connect timeout={self.timeout})",
137 ) from e
138 else:
139 # Adding `from e` messes with coverage somehow, so it's omitted.
140 # See #2386.
141 raise NewConnectionError(
142 self, f"Failed to establish a new connection: {error}"
143 )
144 else: # Defensive: see https://github.com/urllib3/urllib3/pull/3728#pullrequestreview-3816302703
145 raise NewConnectionError(
146 self, f"Failed to establish a new connection: {e}"
147 ) from e
148
149 except OSError as e: # Defensive: PySocks should catch all these.
150 raise NewConnectionError(
151 self, f"Failed to establish a new connection: {e}"
152 ) from e
153
154 return conn
155

Callers

nothing calls this directly

Calls 2

ConnectTimeoutErrorClass · 0.85
NewConnectionErrorClass · 0.85

Tested by

no test coverage detected