def _has_ipv6(host): """ Returns True if the system can bind an IPv6 address. """ sock = None has_ipv6 = False # App Engine doesn't support IPV6 sockets and actually has a quota on the # number of sockets that can be used, so just early out here instead of # creating a socket needlessly. # See https://github.com/urllib3/urllib3/issues/1446 if appengine.is_appengine_sandbox(): return False if socket.has_ipv6: # has_ipv6 returns true if cPython was compiled with IPv6 support. # It does not tell us if the system has IPv6 support enabled. To # determine that we must bind to an IPv6 address. # https://github.com/shazow/urllib3/pull/611 # https://bugs.python.org/issue658327 try: sock = socket.socket(socket.AF_INET6) sock.bind((host, 0)) has_ipv6 = True except Exception: pass if sock: sock.close() return has_ipv6
def __init__(self, config): self.api_key = config["apiKey"] self.auth_domain = config["authDomain"] self.database_url = config["databaseURL"] self.storage_bucket = config["storageBucket"] self.credentials = None self.requests = requests.Session() if config.get("serviceAccount"): scopes = [ 'https://www.googleapis.com/auth/firebase.database', 'https://www.googleapis.com/auth/userinfo.email', "https://www.googleapis.com/auth/cloud-platform" ] service_account_type = type(config["serviceAccount"]) if service_account_type is str: self.credentials = ServiceAccountCredentials.from_json_keyfile_name( config["serviceAccount"], scopes) if service_account_type is dict: self.credentials = ServiceAccountCredentials.from_json_keyfile_dict( config["serviceAccount"], scopes) if is_appengine_sandbox(): # Fix error in standard GAE environment # is releated to https://github.com/kennethreitz/requests/issues/3187 # ProtocolError('Connection aborted.', error(13, 'Permission denied')) adapter = appengine.AppEngineAdapter(max_retries=3) else: adapter = HTTPAdapter(max_retries=3) for scheme in ('http://', 'https://'): self.requests.mount(scheme, adapter)
def _get_http_client(): if appengine.is_appengine_sandbox(): return appengine.AppEngineManager() else: return urllib3.PoolManager()
from cloudinary.cache.responsive_breakpoints_cache import instance as responsive_breakpoints_cache_instance try: from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox except Exception: def is_appengine_sandbox(): return False try: # Python 2.7+ from collections import OrderedDict except ImportError: from urllib3.packages.ordered_dict import OrderedDict if is_appengine_sandbox(): # AppEngineManager uses AppEngine's URLFetch API behind the scenes _http = AppEngineManager() else: # PoolManager uses a socket-level API behind the scenes _http = PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) UPLOAD_LARGE_CHUNK_SIZE = 20000000 def upload(file, **options): params = utils.build_upload_params(**options) return call_cacheable_api("upload", params, file=file, **options) def unsigned_upload(file, upload_preset, **options):
def test_gae_environ(): assert not appengine.is_appengine() assert not appengine.is_appengine_sandbox() assert not appengine.is_local_appengine() assert not appengine.is_prod_appengine() assert not appengine.is_prod_appengine_mvms()
def _get_http_client(): if appengine.is_appengine_sandbox(): return appengine.AppEngineManager() else: return urllib3.PoolManager()
from cloudinary.compat import string_types from urllib3.exceptions import HTTPError from urllib3 import PoolManager try: from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox except Exception: def is_appengine_sandbox(): return False try: # Python 2.7+ from collections import OrderedDict except ImportError: from urllib3.packages.ordered_dict import OrderedDict if is_appengine_sandbox(): # AppEngineManager uses AppEngine's URLFetch API behind the scenes _http = AppEngineManager() else: # PoolManager uses a socket-level API behind the scenes _http = PoolManager() def upload(file, **options): params = utils.build_upload_params(**options) return call_api("upload", params, file=file, **options) def unsigned_upload(file, upload_preset, **options): return upload(file, upload_preset=upload_preset, unsigned=True, **options)
def __init__(self, con_pool_size=1, proxy_url=None, urllib3_proxy_kwargs=None, connect_timeout=5., read_timeout=5.): if urllib3_proxy_kwargs is None: urllib3_proxy_kwargs = dict() self._connect_timeout = connect_timeout sockopts = HTTPConnection.default_socket_options + [ (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) ] # TODO: Support other platforms like mac and windows. if 'linux' in sys.platform: sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 120)) sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 30)) sockopts.append((socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 8)) self._con_pool_size = con_pool_size kwargs = dict(maxsize=con_pool_size, cert_reqs='CERT_REQUIRED', ca_certs=certifi.where(), socket_options=sockopts, timeout=urllib3.Timeout(connect=self._connect_timeout, read=read_timeout, total=None)) # Set a proxy according to the following order: # * proxy defined in proxy_url (+ urllib3_proxy_kwargs) # * proxy set in `HTTPS_PROXY` env. var. # * proxy set in `https_proxy` env. var. # * None (if no proxy is configured) if not proxy_url: proxy_url = os.environ.get('HTTPS_PROXY') or os.environ.get( 'https_proxy') if not proxy_url: if appengine.is_appengine_sandbox(): # Use URLFetch service if running in App Engine mgr = appengine.AppEngineManager() else: mgr = urllib3.PoolManager(**kwargs) else: kwargs.update(urllib3_proxy_kwargs) if proxy_url.startswith('socks'): try: from telegram.vendor.ptb_urllib3.urllib3.contrib.socks import SOCKSProxyManager except ImportError: raise RuntimeError('PySocks is missing') mgr = SOCKSProxyManager(proxy_url, **kwargs) else: mgr = urllib3.proxy_from_url(proxy_url, **kwargs) if mgr.proxy.auth: # TODO: what about other auth types? auth_hdrs = urllib3.make_headers( proxy_basic_auth=mgr.proxy.auth) mgr.proxy_headers.update(auth_hdrs) self._con_pool = mgr
from flask import Flask from urllib3 import PoolManager from urllib3.contrib.appengine import AppEngineManager, is_appengine_sandbox from functools import wraps from requests import request from flask import abort from flask import request from flask import render_template http = AppEngineManager() if is_appengine_sandbox() else PoolManager() if not http: raise ValueError("Could not instantiate HTTTP") app = Flask(__name__) authorization_header = "Authorization" token_urlparam = "token" expected_token_length = 256 auth_service_url = "https://security-dot-training-project-lab.appspot.com/" game_engine_url = "https://gameengine-dot-training-project-lab.appspot.com/" lobby_url = "https://lobbyservice-dot-training-project-lab.appspot.com/" def require_authentication(): def decorator(api_method): @wraps(api_method)