Building a JSON quote server in Falcon

Falcon (http://falconframework.org/) is another microframework written in Python, which was designed to be light, fast, and flexible. I have seen this relatively young project evolve to become something really popular due to its speed, which is impressive, so I'm happy to show you a tiny example using it. We're going to build an API that returns a random quote from the Buddha.

In your ch14 folder, create a new one called falcon. We'll have two files: quotes.py and main.py. To run this example, install Falcon and Gunicorn ($ pip install falcon gunicorn or the full requirements for the book). Falcon is the framework, and Gunicorn (Green Unicorn) is a Python WSGI HTTP Server for Unix (which, in layman's terms, means the technology that is used to run the server).

The Web Server Gateway Interface (WSGI) is a simple calling convention for web servers to forward requests to web applications or frameworks written in Python. If you wish to learn more, please checkout PEP333, which defines the interface.

When you're all set up, start by creating the quotes.py file:

# falcon/quotes.py
quotes = [
"Thousands of candles can be lighted from a single candle, "
"and the life of the candle will not be shortened. "
"Happiness never decreases by being shared.",
...
"Peace comes from within. Do not seek it without.",
...
]

You will find the complete list of quotes in the source code for this book. If you don't have it, you can instead fill in your favorite quotes. Note that not every line has a comma at the end. In Python, it's possible to concatenate strings like that, as long as they are in brackets (or braces). It's called implicit concatenation.

The code for the main application is not long, but it is interesting:

# falcon/main.py
import json
import random
import falcon
from quotes import quotes

class QuoteResource:
def on_get(self, req, resp):
quote = {
'quote': random.choice(quotes),
'author': 'The Buddha'
}
resp.body = json.dumps(quote)

api = falcon.API()
api.add_route('/quote', QuoteResource())

Let's start with the class. In Django we had a get method, in Flask we defined a function, and here we write an on_get method, a naming style that reminds me of Java/C# event handlers. It takes a request and a response argument, both automatically fed by the framework. In its body, we define a dictionary with a randomly chosen quote, and the author information. Then we dump that dictionary to a JSON string and set the response body to its value. We don't need to return anything, Falcon will take care of it for us.

At the end of the file, we create the Falcon application, and we call add_route on it to tie the handler we have just written to the URL we want.

When you're all set up, change to the falcon folder and type:

$ gunicorn main:api  

Then, make a request (or simply open the page with your browser) to http://127.0.0.1:8000/quote. When I did it, I got this JSON in response:

{
quote: "Peace comes from within. Do not seek it without.",
author: "The Buddha"
}

Within the falcon folder, I have left a stress.py module for you, which tests how fast our Falcon code is. See if you can make it work by yourself, it should be very easy for you at this point.

Whatever framework you end up using for your web development, try to keep yourself informed about other choices too. Sometimes you may be in situations where a different framework is the right way to go, and having a working knowledge of different tools will give you an advantage.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset