Meta

Month: December 2015

This tutorial describes how to create a simple Flask application and set it up under Apache on OS X. Flask is a microframework written in Python. The source code is based on the Flask tutorial. The source code for this tutorial can be found on Github.

As in the tutorial on Flask website, we will create the simple Flaskr blog.

First, create a folder for the project at some convenient location: e.g. ~/flaskr.

In the project root, create two folders static and templates.

In the root folder, create a file flaskr.py containing the following code:

Note: As of the time of writing the pip version didn’t seem to work well. On testing the installation there was an error about tutorial.conf not being found, hence my using hg.

Test your installation using command:

python -m cherrypy.tutorial.tut01_helloworld

You should see several lines of text show up with the last line containing the text:

ENGINE Bus STARTED

Now we’re ready to create a project. Create a folder for the project, say ~/Documents/cherrypyapp and change to the directory.

Just like in the tutorial on CherryPy website, we’re going to create a sample app that generates a random string of a user-supplied length (8 by default). The app will allow you edit or delete the string. SQLite db will be used to store the string generated.

The StringGenerator class contains an index() function which handles the home page link. As seen, it just outputs the contents of index.html.

StringGeneratorWebService is a class that provides a RESTful API for creating, updating and deleting a string which we generate in the front end. The setup_database() function creates the user_string table and the cleanup_database() function drops it.

In the conf variable the line ‘request.dispatch’: cherrypy.dispatch.MethodDispatcher() indicates that we will use HTTP request methods that match the name of functions within the StringGeneratorWebService class.

Note: On running the first command you may see an output saying No changes were detected. Ignore it.

You may now create a superuser using the command:

python manage.py createsuperuser

Now that is done, you may start your server using the command:

python manage.py runserver 0.0.0.0:8000

That’s it! You can now view your site by visiting http://localhost:8000 in your browser. You should see the default index page. Using the admin credentials you created earler, you should be able to log in to the admin at http://localhost:8000/admin

Sources

How To Use PostgreSQL with your Django Application on Ubuntu 14.04 | DigitalOcean. https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-django-application-on-ubuntu-14-04

In this post I discuss how to check what program is listening to a port on Ubuntu.

The command to use is

netstat -anp <port number>

The “a” option means that all sockets should be shown, both listening and non-listening ones. The “n” option means numeric. By default netstat translates ports to their service name, making it more difficult to search ports by number. This option solves that. Finally, the “p” option shows the PID and name of the program so that you can tell exactly what’s listening to a port.