def test_client_certificate(self, context_kwargs): # Don't have the server thread do TLS: we'll do it ourselves. self.set_up(secure=False) evt = threading.Event() data = [] def socket_handler(listener): sock = listener.accept()[0] sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_SSLv23, certfile=SERVER_CERT_FILE, keyfile=SERVER_KEY_FILE, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CLIENT_PEM_FILE, server_side=True) data.append(sock.recv(65535)) evt.wait(5) sock.close() self._start_server(socket_handler) # Set up the client context. Don't validate the server cert though. context = init_context(**context_kwargs) context.check_hostname = False context.verify_mode = ssl.CERT_NONE s = socket.create_connection((self.host, self.port)) s, proto = wrap_socket(s, "localhost", ssl_context=context) s.sendall(b'hi') s.close() evt.set() self.tear_down()
def test_client_certificate(self, context_kwargs): # Don't have the server thread do TLS: we'll do it ourselves. self.set_up(secure=False) evt = threading.Event() data = [] def socket_handler(listener): sock = listener.accept()[0] sock = ssl.wrap_socket( sock, ssl_version=ssl.PROTOCOL_SSLv23, certfile=SERVER_CERT_FILE, keyfile=SERVER_KEY_FILE, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CLIENT_PEM_FILE, server_side=True ) data.append(sock.recv(65535)) evt.wait(5) sock.close() self._start_server(socket_handler) # Set up the client context. Don't validate the server cert though. context = init_context(**context_kwargs) context.check_hostname = False context.verify_mode = ssl.CERT_NONE s = socket.create_connection((self.host, self.port)) s, proto = wrap_socket(s, "localhost", ssl_context=context) s.sendall(b'hi') s.close() evt.set() self.tear_down()
def connect(self): """ Connect to the server specified when the object was created. This is a no-op if we're already connected. Concurrency ----------- This method is thread-safe. It may be called from multiple threads, and is a noop for all threads apart from the first. :returns: Nothing. """ #print("connecting to ATS") with self._lock: if self._sock is not None: return sni = self.host if not self.proxy_host: host = self.host port = self.port else: host = self.proxy_host port = self.proxy_port sock = socket.create_connection((host, port)) if self.secure: #assert not self.proxy_host, "Proxy with HTTPS not supported." sock, proto = wrap_socket(sock, sni, self.ssl_context, force_proto=self.force_proto) else: proto = H2C_PROTOCOL log.debug("Selected NPN protocol: %s", proto) assert proto in H2_NPN_PROTOCOLS or proto == H2C_PROTOCOL self._sock = BufferedSocket(sock, self.network_buffer_size) self._send_preamble()
def test_connection_string(self): self.set_up() evt = threading.Event() def socket_handler(listener): sock = listener.accept()[0] evt.wait(5) sock.close() self._start_server(socket_handler) s = socket.create_connection((self.host, self.port)) s, proto = wrap_socket(s, "localhost", force_proto=b"test") s.close() evt.set() assert proto == b"test" self.tear_down()