import signal
import time

import tornado.httpserver
import tornado.ioloop

from django.core.management.base import BaseCommand, CommandError

from chat.tornado_chat import app

class Command(BaseCommand):
    help = 'Starts the Tornado application for message handling.'

    def sig_handler(self, sig, frame):
        """Catch signal and init callback"""
        tornado.ioloop.IOLoop.instance().add_callback(self.shutdown)

    def shutdown(self):
        """Stop server and add callback to stop i/o loop"""
        self.http_server.stop()

        io_loop = tornado.ioloop.IOLoop.instance()
        io_loop.add_timeout(time.time() + 2, io_loop.stop)

    def handle(self, *args, **options):
        self.http_server = tornado.httpserver.HTTPServer(app)
        self.http_server.listen(8888, address="127.0.0.1")

        # Init signals handler
        signal.signal(signal.SIGTERM, self.sig_handler)

        # This will also catch KeyboardInterrupt exception
        signal.signal(signal.SIGINT, self.sig_handler)

        tornado.ioloop.IOLoop.instance().start()

@@ -688,16 +688,234 @@
 tions):%0A
+        if len(args) == 1:%0A            try:%0A                port = int(args%5B0%5D)%0A            except ValueError:%0A                raise CommandError('Invalid port number specified')%0A        else:%0A            port = 8888%0A%0A
         
@@ -1000,20 +1000,20 @@
 .listen(
-8888
+port
 , addres

improve management comand