import http.server, ssl, sys, os, threading
os.chdir('/root/tt')
class H(http.server.SimpleHTTPRequestHandler):
    protocol_version = 'HTTP/1.1'
    def log_message(self, fmt, *a):
        open('/root/tt/access.log', 'a').write('%s %s:%s %s\n' % (__import__('time').strftime('%H:%M:%S'), self.client_address[0], self.server.server_address[1], fmt % a))
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER); ctx.load_cert_chain('c.pem', 'k.pem')
class S(http.server.ThreadingHTTPServer):
    daemon_threads = True; request_queue_size = 256; tls = False
    def process_request_thread(self, request, client_address):
        # TLS handshake happens here, in the per-connection thread: a stalled or junk client can no longer block everyone else
        if self.tls:
            try:
                request.settimeout(10); request = ctx.wrap_socket(request, server_side=True); request.settimeout(120)
            except Exception:
                try: request.close()
                except Exception: pass
                return
        super().process_request_thread(request, client_address)
def run(port, tls):
    s = S(('0.0.0.0', port), H); s.tls = tls; s.serve_forever()
threading.Thread(target=run, args=(80, False), daemon=True).start()
run(443, True)
