Getting Started
Install
The easiest way to install Pycnic is though pip
.
pip install pycnic
Or, for Python 3:
pip3 install pycnic
Now Pycnic is ready to be used.
Making an App
Let's create a new file called quote.py
# quote.py from pycnic.core import WSGI, Handler class QuoteRes(Handler): def get(self): return { "quote":"Cool URIs don't change", "author":"Tim Berners-Lee" } class app(WSGI): routes = [('/', QuoteRes())]
The basic structure of this app is as follows:
- The
QuoteRes
subclass ofHandler
. This exposesget, post, put, delete
, andoptions
methods of your subclass to the client if those methods are implemented. For now, we only care aboutget
. - The
app
subclass ofWSGI
. This is a wsgi class with some configuration options. For now, we only care about routing '/' toQuoteRes
.
Running an App
Since Pycnic is WSGI compliant, running this app can be done a number of ways.
For this example, let's use Gunicorn.
Installing Gunicorn
Gunicorn is available in the Python Package Index, so it can be installed with
pip install gunicorn
Or, for Python 3
pip3 install gunicorn
Hosting with Gunicorn
In the same directory as quote.py
, run
gunicorn quote:app
Your app should now be hosted.
[2015-11-03 13:03:09 -0500] [7292] [INFO] Starting gunicorn 19.3.0 [2015-11-03 13:03:09 -0500] [7292] [INFO] Listening at: http://127.0.0.1:8000 (7292) ...
If you visit http://localhost:8000/ you should see a response like the following:
{"quote": "Cool URIs don't change", "author": "Tim Berners-Lee"}