3

I'll keep this as vague as possible - as it's quite a broad question.

I'm building a payment system within my Django project - and it would be amazing to be able to run my project over a secure server connections. And now were moving into a more forced secure internet with more emphasis on site security with in browser alerts etc I think this is something that needs to be added to the Django core management commands.

I've started to build this functionality inside an application:

management/commands/runsecureserver.py:

import os
import ssl
import sys

from django.core.servers.basehttp import WSGIServer

class SecureHTTPServer(WSGIServer):
    def __init__(self, address, handler_cls, certificate, key):
        super(SecureHTTPServer, self).__init__(address, handler_cls)
        self.socket = ssl.wrap_socket(self.socket, certfile=certificate,
                                      keyfile=key, server_side=True,
                                      ssl_version=ssl.PROTOCOL_TLSv1_2,
                                      cert_reqs=ssl.CERT_NONE)

I'm now wondering - I have the following class that extends from the BaseCommand class from Django's runserver.py where I add my arguments for specifying cert files etc, an inner_run() function which will mimic a lot of the Django runserver inner_run() with added certificate checks, port configurations etc.

class Command(BaseCommand):
    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)
        parser.add_argument(**some argument here***)

However, when running:

$ python manage.py runsecureserver

I receive the following error:

NotImplementedError: subclasses of BaseCommand must provide a handle() method

So, it's telling me I need a handle() method...

Q. What is a handle() method in this context, and what should it do?

Q. Is it enough to simply use the existing handle() method from Django's runserver.py?

2
  • A quick question regarding SSL. Why not run it behind a web server like nginx? Or am I missing something?
    – BitParser
    Aug 25, 2018 at 14:39
  • @TGO Just out of laziness really of moving everything to a staging server, and it helps learn the core Django infrastructure. :)
    – user10261970
    Aug 25, 2018 at 15:02

1 Answer 1

0

https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/

Your file must be within /DjangoProject/YouApp/management/commands/

# custom_command.py
class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('--delta')
        parser.add_argument('--alpha')
        parser.add_argument('--universe')

    def handle(self, *args, **options):
        # argument in a dict here :
        print(options)

        # Do your stuff :)
        pass

python manage.py custom_command --universe=42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.