Django: launch with xinetd

    So, another way to start Django. But why? After all, there are already no launch methods. It would seem for every taste. Justification and description - under the cut.



    Introduction



    I conceived one idea. Namely, use web applications as applications on your computer. Since I work with Python / Django, it is natural to write applications using just them.

    What is needed for this? You need to run web applications with a minimum of unnecessary work. Everything seems to be there, Django can work without a database server (using SQLite), there is a built-in devserver, i.e. You can run web applications without installing and configuring a web server.

    But it turned out that if everything is fine with SQLite, then working with the built-in devserver is not so convenient. It must be run manually, which you will agree, extra body movements. But I would like that when you click on the link everything starts to work automatically.

    Of course, you can run devserver at system startup using startup scripts. But it is somehow ugly, eats resources and slows down the start of the computer. And if there are many such applications?

    So what do you want? In order for the web application to start working when accessed to its address, and in the absence of calls, it would lie quietly to itself on the hard drive.

    Proposed solution



    Then I remembered xinetd. This server is just doing something very similar to my task:

    • Listens to configured ports on the local host.
    • When accessing via a pre-configured port, the desired application starts and transfers the received request to the standard input of the application.
    • The launched application passes the response to its standard output, xinetd gives the response to the client.


    Everything is fine, only there is standard input and output, and Django communicates with the web server using the WSGI protocol.

    Script



    So, you need to write a script that will communicate with xinetd on the one hand via stdin / stdout, and on the other hand, with my Django application, through WSGI.

    The script was written and the first version is posted on GitHub. Maybe he will be interested in someone, welcome.

    You can download the script on GitHub: django-xinetd

    Installation and configuration are described in README.

    Conclusion



    Of course, the performance of such a solution is lower than using a web server, but I’m quite happy.

    This is the first version and of course very crude. But for the simplest cases, you can use it.

    I almost forgot, Linux (Ubuntu) is used as a platform. xinetd is installed with the command:

    apt-get install xinetd
    


    The script was written and tested with Django v1.4. This is important, since in previous versions the work with static files differs. With earlier versions, static files will not be served.

    Also popular now: