Best Python code snippet using localstack_python
proxy_server.py
Source:proxy_server.py
...94 proxy = FuncThread(_run)95 TMP_THREADS.append(proxy)96 proxy.start()97 return proxy98def _save_cert_keys(client_cert_key: Tuple[str, str]) -> Tuple[str, str]:99 """100 Save the given cert / key into files and returns their filename101 :param client_cert_key: tuple with (client_cert, client_key)102 :return: tuple of paths to files containing (client_cert, client_key)103 """104 cert_file = client_cert_key[0]105 if not os.path.exists(cert_file):106 cert_file = new_tmp_file()107 save_file(cert_file, client_cert_key[0])108 key_file = client_cert_key[1]109 if not os.path.exists(key_file):110 key_file = new_tmp_file()111 save_file(key_file, client_cert_key[1])112 return cert_file, key_file113def _do_start_ssl_proxy(114 port: int,115 target: PortOrUrl,116 target_ssl=False,117 client_cert_key: Tuple[str, str] = None,118 bind_address: str = "0.0.0.0",119):120 """121 Starts a tcp proxy (with tls) on the specified port122 :param port: Port the proxy should bind to123 :param target: Target of the proxy. If a port, it will connect to localhost:124 :param target_ssl: Specify if the proxy should connect to the target using SSL/TLS125 :param client_cert_key: Client certificate for the target connection. Only set if target_ssl=True126 :param bind_address: Bind address of the proxy server127 """128 import pproxy129 from localstack.services.generic_proxy import GenericProxy130 if ":" not in str(target):131 target = f"127.0.0.1:{target}"132 LOG.debug("Starting SSL proxy server %s -> %s", port, target)133 # create server and remote connection134 server = pproxy.Server(f"secure+tunnel://{bind_address}:{port}")135 target_proto = "ssl+tunnel" if target_ssl else "tunnel"136 remote = pproxy.Connection(f"{target_proto}://{target}")137 if client_cert_key:138 # TODO verify client certs server side?139 LOG.debug("Configuring ssl proxy to use client certs")140 cert_file, key_file = _save_cert_keys(client_cert_key=client_cert_key)141 remote.sslclient.load_cert_chain(certfile=cert_file, keyfile=key_file)142 args = dict(rserver=[remote])143 # set SSL contexts144 _, cert_file_name, key_file_name = GenericProxy.create_ssl_cert()145 for context in pproxy.server.sslcontexts:146 context.load_cert_chain(cert_file_name, key_file_name)147 loop = ensure_event_loop()148 handler = loop.run_until_complete(server.start_server(args))149 try:150 loop.run_forever()151 except KeyboardInterrupt:152 print("exit!")153 handler.close()154 loop.run_until_complete(handler.wait_closed())155 loop.run_until_complete(loop.shutdown_asyncgens())156 loop.close()157def _do_start_ssl_proxy_with_client_auth(158 port: int, target: PortOrUrl, client_cert_key: Tuple[str, str]159):160 # prepare cert files (TODO: check whether/how we can pass cert strings to requests.request(..) directly)161 cert_params = _save_cert_keys(client_cert_key)162 # start proxy163 requests_kwargs = {"cert": cert_params}164 result = _do_start_ssl_proxy_with_listener(port, target, requests_kwargs=requests_kwargs)165 return result166def _do_start_ssl_proxy_with_listener(167 port: int, target: PortOrUrl, requests_kwargs: Dict[str, Any] = None168):169 target = f"http://localhost:{target}" if isinstance(target, int) else target170 base_url = f"{'https://' if '://' not in target else ''}{target.rstrip('/')}"171 requests_kwargs = requests_kwargs or {}172 # define forwarding listener173 class Listener(ProxyListener):174 def forward_request(self, method, path, data, headers):175 # send request to target...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!