python-urlopen 0.1.6__tar.gz → 0.1.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-urlopen
3
- Version: 0.1.6
3
+ Version: 0.1.6.1
4
4
  Summary: Python urlopen wrapper.
5
5
  Home-page: https://github.com/ChenyangGao/python-modules/tree/main/python-urlopen
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-urlopen"
3
- version = "0.1.6"
3
+ version = "0.1.6.1"
4
4
  description = "Python urlopen wrapper."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
@@ -122,18 +122,15 @@ class KeepAliveBaseHTTPHandler(AbstractHTTPHandler):
122
122
 
123
123
 
124
124
  class KeepAliveHTTPHandler(HTTPHandler, KeepAliveBaseHTTPHandler):
125
- pass
125
+ pool: ConnectionPool = CONNECTION_POOL
126
126
 
127
127
 
128
128
  class KeepAliveHTTPSHandler(HTTPSHandler, KeepAliveBaseHTTPHandler):
129
- pass
129
+ pool: ConnectionPool = CONNECTION_POOL
130
130
 
131
131
 
132
- _pool = CONNECTION_POOL
133
132
  _http_handler = KeepAliveHTTPHandler()
134
- _http_handler.pool = _pool
135
133
  _https_handler = KeepAliveHTTPSHandler(context=_create_unverified_context())
136
- _https_handler.pool = _pool
137
134
  _cookies = CookieJar()
138
135
  _opener: OpenerDirector = build_opener(
139
136
  _http_handler,
@@ -163,6 +160,7 @@ def urlopen(
163
160
  cookies: None | CookieJar | BaseCookie = None,
164
161
  timeout: None | Undefined | float = undefined,
165
162
  opener: None | OpenerDirector = _opener,
163
+ pool: None | ConnectionPool = None,
166
164
  **_,
167
165
  ) -> HTTPResponse:
168
166
  if isinstance(url, Request):
@@ -205,52 +203,62 @@ def urlopen(
205
203
  cookies = getattr(opener, "cookies", None)
206
204
  if cookies and "cookie" not in headers_:
207
205
  headers_["cookie"] = cookies_to_str(cookies)
208
- if context is None:
209
- if opener is None:
210
- handlers.append(copy(_https_handler))
206
+ add_handler = handlers.append
207
+ if opener is None:
208
+ http_handler = copy(_http_handler)
209
+ if context is None:
210
+ https_handler = copy(_https_handler)
211
211
  else:
212
- for i, handler in enumerate(handlers):
213
- if isinstance(handler, KeepAliveHTTPSHandler):
214
- break
215
- elif isinstance(handler, HTTPSHandler):
216
- handlers[i] = copy(_https_handler)
217
- break
218
- else:
219
- handlers.append(copy(_https_handler))
212
+ https_handler = KeepAliveHTTPSHandler(context=context)
213
+ if pool is not None:
214
+ http_handler.pool = pool
215
+ https_handler.pool = pool
216
+ add_handler(http_handler)
217
+ add_handler(https_handler)
220
218
  else:
221
- https_handler = KeepAliveHTTPSHandler(context=context)
222
- https_handler.pool = _pool
223
- if opener is None:
224
- handlers.append(https_handler)
219
+ for i, handler in enumerate(handlers):
220
+ if isinstance(handler, KeepAliveHTTPSHandler):
221
+ handler = handlers[i] = copy(handler)
222
+ if context is not None:
223
+ setattr(handler, "_context", context)
224
+ break
225
+ elif isinstance(handler, HTTPSHandler):
226
+ handler = handlers[i] = KeepAliveHTTPSHandler(
227
+ debuglevel=getattr(handler, "_debuglevel"),
228
+ context=getattr(handler, "_context") if context is None else context,
229
+ )
230
+ break
225
231
  else:
226
- for i, handler in enumerate(handlers):
227
- if isinstance(handler, HTTPSHandler):
228
- handlers[i] = https_handler
229
- break
230
- else:
231
- handlers.append(https_handler)
232
- if opener is None:
233
- handlers.append(copy(_http_handler))
234
- else:
232
+ handler = copy(_https_handler)
233
+ if context is not None:
234
+ setattr(handler, "_context", context)
235
+ add_handler(handler)
236
+ if pool is not None:
237
+ handler.pool = pool
235
238
  for i, handler in enumerate(handlers):
236
239
  if isinstance(handler, KeepAliveHTTPHandler):
240
+ handler = handlers[i] = copy(handler)
237
241
  break
238
242
  elif isinstance(handler, HTTPHandler):
239
- handlers[i] = copy(_http_handler)
243
+ handler = handlers[i] = KeepAliveHTTPHandler(
244
+ debuglevel=getattr(handler, "_debuglevel"))
240
245
  break
241
246
  else:
242
- handlers.append(copy(_http_handler))
247
+ handler = copy(_http_handler)
248
+ add_handler(handler)
249
+ if pool is not None:
250
+ handler.pool = pool
243
251
  if cookies and (opener is None or all(
244
252
  h.cookies is not cookies
245
253
  for h in getattr(opener, "handlers") if isinstance(h, HTTPCookieProcessor)
246
254
  )):
247
- handlers.append(HTTPCookieProcessor(cookies))
255
+ add_handler(HTTPCookieProcessor(cookies))
248
256
  response_cookies = CookieJar()
249
257
  if cookies is None:
250
258
  cookies = response_cookies
251
- handlers.append(HTTPCookieProcessor(response_cookies))
259
+ add_handler(HTTPCookieProcessor(response_cookies))
252
260
  if not follow_redirects:
253
- handlers.append(NoRedirectHandler())
261
+ add_handler(NoRedirectHandler())
254
262
  opener = build_opener(*handlers)
255
263
  setattr(opener, "cookies", cookies)
256
264
  try:
File without changes