Simple python daemon

Below is a very simple python daemon script, which will print "Hello World!".

Make sure you install python-daemon in your linux machine.

sudo apt-get install python-daemon

Here’s the script.

#!/usr/bin/env python
import time
import subprocess
from daemon import runner

class Remote():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/remote.pid'
self.pidfile_timeout = 5
def run(self):
while True:
print "Hello World!"
time.sleep(15)
Remote = Remote()
daemon_runner = runner.DaemonRunner(Remote)
daemon_runner.do_action()

Integrate django, uwsgi and nginx to run python web application

I am currently working on setting up django-cms in ubuntu 12.04.django is a popular python framework used widely for web application.uwsgi is a plugin used to connect python app to web server.

We can run a python(django) application without any webserver as below in virtualenv:

python manage.py runserver 127.0.0.1:8000

Integrating the application with web server results better performance and accessibility.Here’s how I did it.

Created the application directory in /usr/share/intranet/space.site.com, and installed virtualenv inside it.Following are the files and directories resides at top level in space.site.com.

env
manage.py
project.db
space
static

virtualenv installed files are in env and the django app(django-cms) is installed in space.

Install uwsgi
sudo apt-get install uwsgi

Create uwsgi configuration file space.site.com.ini in /etc/uwsgi/apps-available. Add below entries to the file.

[uwsgi]
virtualenv=/usr/share/intranet/space.site.com/env
thread=3
master=1
env = DJANGO_SETTINGS_MODULE=space.settings
module = django.core.handlers.wsgi:WSGIHandler()
chdir = /usr/share/intranet/space.site.com
socket = /run/uwsgi/app/space.site.com/space.site.com.socket
logto = /var/log/uwsgi/space.site.com.log

Note that my app name is space.

Now create link to apps-enabled directory.

ln -s /etc/uwsgi/apps-available/space.site.com.ini /etc/uwsgi/apps-enabled/space.site.com.ini

Create nginx configuration file named space for the app in /etc/nginx/sites-available.Add below entries to it.

server {
listen 80;
server_name space.site.com;

location / {
uwsgi_pass unix:///run/uwsgi/app/space.site.com/space.site.com.socket;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;

}

location /static {
root /usr/share/intranet/space.site.com/space/static;
index index.html index.htm;

}

}

Now create link to sites-enabled directory.
ln -s /etc/nginx/sites-available/space /etc/nginx/sites-enabled/space

Restart nginx
sudo service nginx restart

Restart uwsgi
service uwsgi restart

Access the application in browser by typing http://space.site.com/.It should open the site.

For any error check the log file.

Here are few errors I came across.

Tue Nov 11 07:02:06 2014 - opendir(): No such file or directory [uwsgi.c line 471]

This issue was fixed by creating the directory below.
mkdir -p /usr/lib/uwsgi/plugins

unavailable modifier requested: 0

This issue was fixed by installing uwsgi-plugin-python
sudo apt-get install uwsgi-plugin-python

django CMS installation error

django CMS is a python based cms which can be used as intranet application.While installing it in python virtualenv with djangocms-installer, I came accross the error below.

This was tested in Ubuntu 12.04.4 LTS.

EnvironmentError: Pillow is not compiled with JPEG support, see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html

Steps below can be followed to fix this issue.

sudo apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev
sudo pip uninstall pillow
sudo pip install pillow

Disable suexec in apache

If you are trying to get python, perl or cgi scripts to work with Apache and you come across this error.

[Thu Sep 25 09:36:28 2014] [error] [client x.x.x.x] suexec policy violation: see suexec log for more details
[Thu Sep 25 09:36:28 2014] [error] [client x.x.x.x] Premature end of script headers: x.py

you can fix this either by configuring suexec properly(which require efforts beyond this discussion) or by disabling suexec.

Disable suexec in apache(centOS)

1. Run command (to get suexec location):

httpd -V | grep -i suexec

-D SUEXEC_BIN=”/usr/sbin/suexec”

2.Move suexec binary:

mv  /usr/sbin/suexec  /usr/sbin/suexec.unused

3.Restart apache:

service httpd restart

Use logging in python script(quick note)

Python(tested in 2.7) logging module can be used for logging in a file or printing on the screen while running the script or scheduling it  via cron.

1.Write in a file.

import logging

logging.basicConfig(filename='/your/log/file', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

response='<some execution output>'

logging.debug(response)

logging.debug("This is a sample log")

2.Print on screen

import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

response='<some execution output>'

logging.debug(response)

logging.debug("This is a sample log")

For more information visit:

http://inventwithpython.com/blog/2012/04/06/stop-using-print-for-debugging-a-5-minute-quickstart-guide-to-pythons-logging-module/

Equality Vs Identity in python

Python has conditional operators like “==” used for equality and “is” used for identity.In order to avoid confusion here is the example which show the difference.

>>> a = 'wild'
>>> b = ' '.join(['w', 'i', 'l', 'd'])
>>> b
'wild'
>>> a==b
True
>>> a is b
False
>>> id(a)
140156328679344
>>> id(b)
140156328679248

Mutable Vs Immutable types in python

Python represents all its data as objects.Some of the objects like lists and dictionaries are mutable, meaning you can change their content without changing their identity.Other objects like integers, floats,strings, tuples etc are the objects which cannot be changed.

 

Below is an example showing strings are immutable objects

>>> s="abc"
>>> id(s)
3077106312L
>>> s[0]
'a'
>>> s[1]
'b'
>>> s[0]="P"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s="qwe"
>>> id(s)
3076980912L
>>> s +="jkl"
>>> id(s)
3076970560L
>>>

 

Below is an example showing lists are mutable objects

>>> d=[1, 5, 6]
>>> id(d)
3076965996L
>>> d[0]
1
>>> d[0]=3
>>> d[0]
3
>>> id(d)
3076965996L

Vim as python ide

1. Set the below parameters in /etc/vimrc to activate auto indent and other stuff

filetype plugin indent on

set cindent

autocmd FileType python setlocal foldmethod=indent smartindent shiftwidth=4 ts=4 et cinwords=if,elif,else,for,while,try,except,finally,def,class

2.Set autocmd to run python script from vim itself edit /etc/vimrc and add following line (F9 and ENTER to execute). ” :w   !python”  would do without autocmd

autocmd FileType python nnoremap <buffer> <F9> :exec ‘!python’ shellescape(@%, 1)<cr>