code: def serve_websocket(request, port):
      
      env = request.environ
  
      # Send HTTP response 101 Switch Protocol downstream
      uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', ''))
  
      # Map the websocket URL to the upstream localhost:4000x Notebook instance
      parts = urlparse(request.url)
      parts = parts._replace(scheme="ws", netloc="localhost:{}".format(port))
      url = urlunparse(parts)
  
      # Proxy initial connection headers
      headers = [(header, value) for header, value in request.headers.items() if header.lower() in CAPTURE_CONNECT_HEADERS]
  
      logger.info("Connecting to upstream websockets: %s, headers: %s", url, headers)
  
      ws = ProxyClient(url, headers=headers)
      ws.connect()
  
      # TODO: Will complain loudly about already send headers - how to abort?
      return httpexceptions.HTTPOk()
docs: Start UWSGI websocket loop and proxy.