FredLawl's Blog

Breaking away from PHP

| Comments

Today marked the day where I am took my first steps to expanding my horizons through learning the Python language. A couple of weeks ago, during a meeting at work, a few other developers and I discussed plans to design and develop a new CRM for the company.

The CRM

The CRM was broken up into three pieces: a database, a web-service, and a front-end. The database piece is to store our data such as: users, tasks, events, and discussions. The web-service piece is for syncing up various web services across some of the online systems the company uses. The front-end part of the CRM is a means to provide a easy interface to enter time, create projects, and display information stored within the database.

Choice to move

My task was to lead a small team in the development of the web-service portion of the CRM. The various programming languages that came across the table were: PHP, Ruby, and Python. The developers of my team, including myself, are all extremely comfortable using PHP—since we use that as our primary go-to language for web development. Though we were comfortable with PHP, we saw this as an opportunity to expand our knowledge and we eventually decided on using Python for this portion of the CRM.

We choose Python over PHP for a couple of reasons. The primary reason we chose Python was because of the scope of the project. The scope calls for parallel processing because the web-service needs to sync data across many web-based applications. The more services that are added to the project, the slower the syncing and processing. Since PHP does not handle forking and threading very nicely, Python seemed to be the optimal choice. The second reason we chose Python was because of the ability to add technologies on the fly. This will be important through the development process.

Getting Started

A part of the web-service piece is to be able to accept and parse webhooks that are passed to it by the web services. Clearly, there had to be a way to serve python files over the internet. Instead of designing a system to accept and serve python files, I came to the conclusion of using the mod_wsgi for Apache combined with CherryPy. To avoid re-inventing the wheel.

Since this was the first time ever considering Python for a web project, I had to overcome the learning curve of installing and setting up wsgi_mod. To some, this may be an easy task, but as a first-timer I had some difficulties.

I did not begin the process by installing it on my own hosted server, instead I wanted to try installing it on my local development. Since MacOSX is my primary development platform I did the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
$ brew tap homebrew/apache
$ brew install mod_wsgi
$ cd /etc/apache2
$ subl . # Because I use sublime text 2 as my editor of choice

# Copied and pasted:
# LoadModule wsgi_module /usr/local/Cellar/mod_wsgi/3.4/libexec/mod_wsgi.so
# into http.conf

# Opened up my vhosts file and added, based off recommended configurations:
# <VirtualHost *:80>
#        ServerName python.local
#        ServerAlias www.python.local
#        DocumentRoot "/Users/fredlawl/Sites/python/www"
#
#        Alias /robots.txt /Users/fredlawl/Sites/python/www/robots.txt
#        Alias /favicon.ico /Users/fredlawl/Sites/python/www/favicon.ico
#        Alias /media/ /Users/fredlawl/Sites/python/www/media/
#
#        <Directory /Users/fredlawl/Sites/python/www>
#                Order allow,deny
#                Allow from all
#        </Directory>
#
#        WSGIDaemonProcess python.local processes=2 threads=15 display-name=%{GROUP}
#        WSGIProcessGroup python.local
#        WSGIScriptAlias / /Users/fredlawl/Sites/python/wsgi-scripts/app.wsgi
#
#        <Directory /Users/fredlawl/Sites/python/wsgi-scripts>
#                Order allow,deny
#                Allow from all
#        </Directory>
# </VirtualHost>
# ... then saved.

$ cd /Users/fredlawl/Sites
$ mkdir python
$ cd python
$ mkdir www
$ mkdir wsgi-scripts
$ cd wsgi-scripts
$ nano app.wsgi

# pasted in the following:
# def application(environ, start_response):
#     status = '200 OK'
#     output = 'Hello World!'
#
#     response_headers = [('Content-type', 'text/plain'),
#                         ('Content-Length', str(len(output)))]
#     start_response(status, response_headers)
#
#     return [output]

$ sudo apachectl restart
$ sudo nano /etc/hosts

# added in
# 127.0.0.1     python.local

And there it was…Hello World! was displayed on my screen at http://python.local/

I have not gotten into the installation of CherryPy just yet, but that is a post for another day.

Comments