code: def run_twisted(self, args):
          
          server = self
  
          from twisted.internet import reactor
          from twisted.web import resource
          from twisted.web.static import File
          from twisted.web.server import Site
          from autobahn.twisted.websocket import (WebSocketServerFactory,
                                                  WebSocketServerProtocol)
          from autobahn.twisted.resource import WebSocketResource
  
          class DevWebSocketHandler(WebSocketServerProtocol):
              def onConnect(self, request):
                  super(DevWebSocketHandler, self).onConnect(request)
                  server.on_open(self)
  
              def onMessage(self, payload, isBinary):
                  server.on_message(self, payload)
  
              def onClose(self, wasClean, code, reason):
                  super(DevWebSocketHandler,self).onClose(wasClean, code, reason)
                  server.on_close(self)
  
              def write_message(self, message, binary=False):
                  self.sendMessage(message, binary)
  
          #: Set the call later method
          server.call_later = reactor.callLater
          server.add_callback = reactor.callFromThread
  
          factory = WebSocketServerFactory(u"ws://0.0.0.0:{}".format(self.port))
          factory.protocol = DevWebSocketHandler
  
          class MainHandler(resource.Resource):
              def render_GET(self, req):
                  return str(server.index_page)
  
          root = resource.Resource()
          root.putChild("", MainHandler())
          root.putChild("dev", WebSocketResource(factory))
          reactor.listenTCP(self.port, Site(root))
          print("Twisted Dev server started on {}".format(self.port))
          reactor.run()
docs: Twisted dev server implementation