Posts Tagged django

Cherokee / Django tip: Timeout value

The default Cherokee timeout value is 15 seconds. I write server control dashboards which may have views which take longer than 15 seconds to render because they launch processes etc. Using Jquery / Ajax, I noticed these processes would spawn a bunch. This is because the of the default cherokee timeout. I increased this to 2400 seconds and now my problem went away. Hope that helps someone.

Tags: , ,

The Cherokee Webserver

Cherokee Webserver Logo

Cherokee Webserver Logo

After seeing some of the performance graphs on Alvaro’s blog, I decided to give Cherokee a shot with a project I am working on. The project I am working on has a web browser on kiosk machines, and runs Django on the backend. I was in need of a fast, secure web server. I have always used Apache in the past, and have used Nginx, so I figured I should investigate Cherokee.
Read the rest of this entry »

Tags: , , , , ,

Python Commands Module

Python logoPython Commands Module

The Python module ‘commands’ allows commands to be ran on the system. I frequently use this in combination with Django to launch processes.

Get a directory listing:

#!/usr/bin/python
import commands
status, output = commands.getstatusoutput('ls')
print "Status: " + str(status)
print "Output: " + str(output)

This program will print out the directory contents of where you ran it from. An important thing to note about programs in Linux in general is that on success they return 0, and anything not 0 is probably an error. I commonly check the status of the returned code:

Check if status is not 0:

if status != 0:
    print "An error occurred!"

This allows us to do some really fancy things with this module. For instance, I created an application that is a django app that provides the user a menu to launch programs. This runs on their Firefox browser, but will launch the programs on their system. Each system runs it’s own Cherokee webserver locally, which is extremely lightweight and fast. To install Cherokee to use django I followed this guide: http://www.cherokee-project.com/doc/cookbook_django.html

The users run as the user ‘autologin’, however the webserver runs under the user ‘www’. This is probably a bit hackish, but it works. What I do is copy autologin’s .Xauthority file to the www user’s home directory, then launch the program as autologin using sudo.

Launch a local X program from a django program:

def launchAutologinXProgram(commands):
    status, output = commands.getstatusoutput('sudo cp /home/autologin/.Xauthority /home/www/')
    os.spawnl(os.P_WAIT, "/usr/bin/sudo", "sudo", "-u", "autologin", "-H", "/bin/bash", "-c", 'export DISPLAY=":0.0"; . /etc/profile; nohup ' + str(commands) + ' &')

Obviously you want to check the status variable and make sure it completed. To do this you need to update your sudoers file to make the www user be able to run commands as autologin. This is for a kiosk type setup, so www and autologin don’t have sensitive data, I am sure there is a more secure way to do it. But it sure is fun.

Tags: , , ,