2This module provides a pool manager that uses Google App Engine's
3`URLFetch Service <https://cloud.google.com/appengine/docs/python/urlfetch>`_.
7 from pip._vendor.urllib3 import PoolManager
8 from pip._vendor.urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox
10 if is_appengine_sandbox():
11 # AppEngineManager uses AppEngine's URLFetch API behind the scenes
12 http = AppEngineManager()
14 # PoolManager uses a socket-level API behind the scenes
17 r = http.request('GET', 'https://google.com/')
19There are `limitations <https://cloud.google.com/appengine/docs/python/\
20urlfetch/#Python_Quotas_and_limits>`_ to the URLFetch service and it may not be
21the best choice for your application. There are three options for using
22urllib3 on Google App Engine:
241. You can use :class:`AppEngineManager` with URLFetch. URLFetch is
25 cost-effective in many circumstances as long as your usage is within the
272. You can use a normal :class:`~urllib3.PoolManager` by enabling sockets.
28 Sockets also have `limitations and restrictions
29 <https://cloud.google.com/appengine/docs/python/sockets/\
30 #limitations-and-restrictions>`_ and have a lower free quota than URLFetch.
31 To use sockets, be sure to specify the following in your ``app.yaml``::
34 GAE_USE_SOCKETS_HTTPLIB : 'true'
363. If you are using `App Engine Flexible
37<https://cloud.google.com/appengine/docs/flexible/>`_, you can use the standard
38:class:`PoolManager` without any configuration or special environment variables.
41from __future__
import absolute_import
47from ..exceptions
import (
56from ..request
import RequestMethods
57from ..response
import HTTPResponse
60from .
import _appengine_environ
81 Connection manager for Google App Engine sandbox applications.
83 This manager uses the URLFetch service directly instead of using the
84 emulated httplib, and is subject to URLFetch limitations as described in
85 the App Engine documentation `here
86 <https://cloud.google.com/appengine/docs/python/urlfetch>`_.
88 Notably it will raise an :class:`AppEnginePlatformError` if:
89 * URLFetch is not available.
90 * If you attempt to use this on App Engine Flexible, as full socket
92 * If a request size is more than 10 megabytes.
93 * If a response size is more than 32 megabytes.
94 * If you use an unsupported request method such as OPTIONS.
96 Beyond those cases, it will raise normal urllib3 errors.
103 validate_certificate=True,
104 urlfetch_retries=True,
108 "URLFetch is not available in this environment."
112 "urllib3 is using URLFetch on Google App Engine sandbox instead "
113 "of sockets. To use sockets directly instead of URLFetch see "
114 "https://urllib3.readthedocs.io/en/1.26.x/reference/urllib3.contrib.html.",
115 AppEnginePlatformWarning,
139 timeout=Timeout.DEFAULT_TIMEOUT,
151 headers=headers
or {},
152 allow_truncated=
False,
161 if "too large" in str(e):
163 "URLFetch request too large, URLFetch only "
164 "supports requests up to 10mb in size.",
170 if "Too many redirects" in str(e):
176 "URLFetch response too large, URLFetch only supports"
177 "responses up to 32mb in size.",
186 "URLFetch does not support method: %s" % method, e
190 response, retries=retries, **response_kw
195 if redirect_location:
205 method, url, response=http_response, _pool=self
207 except MaxRetryError:
213 log.debug(
"Redirecting %s -> %s", url, redirect_location)
214 redirect_url = urljoin(url, redirect_location)
252 if content_encoding ==
"deflate":
258 if transfer_encoding ==
"chunked":
277 original_response=original_response,
287 "URLFetch does not support granular timeout settings, "
288 "reverting to total or default URLFetch timeout.",
289 AppEnginePlatformWarning,
300 "URLFetch only supports total retries and does not "
301 "recognize connect, read, or redirect retry parameters.",
302 AppEnginePlatformWarning,
_get_retries(self, retries, redirect)
__init__(self, headers=None, retries=None, validate_certificate=True, urlfetch_retries=True)
__exit__(self, exc_type, exc_val, exc_tb)
_get_absolute_timeout(self, timeout)
urlopen(self, method, url, body=None, headers=None, retries=None, redirect=True, timeout=Timeout.DEFAULT_TIMEOUT, **response_kw)
_urlfetch_response_to_http_response(self, urlfetch_resp, **response_kw)
urlopen(self, method, url, body=None, headers=None, encode_multipart=True, multipart_boundary=None, **kw)