Automatic setup of Django 1.4, NGinx and GUnicorn on EC2 using Bellatrix

Some time ago I wrote some lengthy instructions on how to configure Django, NGinx and Green Unicorn on an brand new Ubuntu EC2 instance. Now, I want to show how to automate the same tasks using a command line tool called Bellatrix. We will also provide a Jenkins (formerly Hudson) pipeline so we have, if we want, a nice display for the UI.

Continue reading

Setting up Django 1.3 + NGinx 1.0.5 + Green Unicorn 0.13 in an Ubuntu 11.10 EC2 instance

Django has become the de-facto web framework for Python. Although, since Django just specializes in dynamic content, you have to combine it (at least in production) with an HTTP server to serve static content such as css, javascript files and images files. In the past, the communication protocol between Python web applications was CGI, FastCGI or mod_python. But after PEP-333 was accepted the faster and more efficient WSGI became the standard.

Green Unicorn is a Python WSGI HTTP Server for UNIX. Its combination with the high performance HTTP server NGinx is gaining lot of momentum in the Python community.

From “man gunicorn”…

Green Unicorn (gunicorn) is an HTTP/WSGI server designed to serve
fast clients or sleepy applications. That is to say; behind a
buffering front-end server such as nginx or lighttpd.


We are describing here how to combine a Django application with Green Unicorn and Nginx within a pristine EC2 Ubuntu 11.10 image.
Continue reading

Getting reTweets from Python

In this snippet I will show how to use the Tweepy library to get statistics from Tweeter.

The script will read a sample feed from the Twitter Streaming API ( and perform the following actions:

* Reads the sample feed
* Notes the number of retweets seen
* Tracks the number of times posts have been retweeted, and
* Produces an hourly report of the 20 most frequently retweeted posts

Continue reading

Concurrency in Hudson

How to organize and avoid concurrency problems in big projects, splitted in multiple jobs and chained to each other in Hudson?

If you have a lengthy process, it will be a good practice to split it in multiple tasks, having each calling the next one, like in the (somewhat simplified) image on the left.

Problem number one: How to avoid Clean and Build (for instance) running at the same time?

As (almost) always, “you have a plugin for that” in Hudson. This time is Locks and Latches, which will let you create a lock, shared by all the jobs you want. Only the job that has the lock, will be executed.

So far, so good, but, where is the problem?

Continue reading

Open ID login with Google App Engine + Django

Hi!, this a clean and simple sample test of a web app using Open ID login in Google App Engine + Django.

This application combines with slight modifications, the following projects:

  • Djangoappengine:
  • Django-nonrel – Changes to Text fields to Chars, due to lack of filtering support in the firsts
  • OpenID integration: – Small change in the login view template, in order to work with standard Cross site forgery protection
  • Python OpenID library:

Just remember to add {% csrf_token %} within the login form of your own view.


How to replace a substring using regex in Python

The problem: You match a string with your regex, but you need to replace just a portion of it. How could we replace it?
The trick is simple, put the text you want to replace within “()” which means “group” in regex language. If the regex worked, you could replace just that portion by using Python match information, like in this example:

#the first group contains the expression we want to replace
pat = "word1s(.*)sword2"
test = "word1 will never be a word2"
repl = "replace"

import re
m =,test)

if m and m.groups() > 0:
  line = test[0:m.start(1)] + repl + test[m.end(1):len(test)]
  print line
  print "the pattern didn't capture any text"

This will print: ‘word1 will never be a word2

The group to be replaced could be located in any position of the string.