enthus1ast
enthus1ast

Reputation: 2109

How to write a html gui (django) for a python server module

What's the way to go to build a HTML gui for eg a multiplexed tcp server in python?

I am familiar with building websites with Django, but the thing i don't understand is, how is the tcp server part communicating with the Django related views? How could i implement the data sharing (do i see the wood for the trees)? The problem for me is the mapping between the stateless "get an leave" and the "state full" py module "running as a daemon".

greetings

edit my standalone application skeleton:

#!/usr/bin/python

from django.core.management import setup_environ
import settings
setup_environ(settings)

from myapp.models import fanzy


def main():
    for each in fanzy.objects.all():
        print each.id, each.foo


if __name__ == '__main__':
    main()

Upvotes: 1

Views: 1065

Answers (2)

André Caron
André Caron

Reputation: 45224

It's not always as easy as importing the libraries, mostly because process lifetime. For example, if you run Django through CGI with 1 request per process, then your TCP server won't stay alive between views. Similarly, if you use multiple processes to handle requests (e.g. using FastCGI), then you'll have several servers running at the same time.

If you want to have permanent network connections alive independent of request lifetimes, you'll need to run the TCP server in an external (daemon) process. This is the standard procedure for some caching schemes, where all your Django processes share cached data via a single deamon (e.g. Redis).

Basically, you have two approaches.

Global connection

Either establish a connection per Django process (if you have more than one) as a global object and forward requests to this from your view. This is most convenient if your TCP server is coded to handle multiple requests per connection. However, you'll have problems if your Django process is multi-threaded.

Connection per request

If your TCP server can accept multiple short-lived connections, this is also a viable approach. Just open the connection for the lifetime of your view. If this object is used often enough, you can even add some piece of middleware that opens up the connection and stores it in the request object.

Upvotes: 2

Gareth
Gareth

Reputation: 1450

Django is just Python, so anything you've written in Python can be imported and referenced in the 'views' that you write for Django to serve back as HTTP responses.

In answer to another part of your question, the way a HTTP server handling TCP connections communicates with the python framework is most commonly through a protocol called WSGI. This is a good place to get more knowledge about the principles of WSGI. This is another.

With regards to running a background process and serving up a view of that processes' activities, it may be better to keep the two problems separate. You could write data to a file or a database and then access and serve this data via your web application.

These are just general comments, because your question is not totally clear. Please feel free to respond with further questions.

Upvotes: 2

Related Questions