Let me state that upfront: I’m a Python newbie. I started playing with slightly over half a year ago as a way to expand my view. Lately I was looking for something new to try among simple scripts that do this or that. I thought something web based is a good way to start. Web is now everywhere and it’s easier than packing up some desktop or mobile application (IMO).
So I started some small research for some web frameworks. Given I’m a newbie I wanted something smart but bare enough to focus on result and not on plumbing. After research where my criteria are obviously skewed I found CherryPy. Reading some tutorials and examples I liked the way it looks and it seemed intuitive.
After some playing locally and on my Raspberry Pi I wanted to do some “real” (don’t miss the quotes ;)) stuff. Azure or Azure Websites to be precise was my target. Mostly because all my fun Python projects are using Azure Storage for something.
Again some research and learning (I love how steep the learning curve is for something completely new for me). Going through documentation on Azure I spotted something I knew I saw in CherryPy’s documentation - WSGI. Apparently you can create WSGI compatible application from CherryPy application in just single line of code. Awesome. Azure seemes to be doing something with it as well so let’s jump in.
So what do you need. First you need
web.config files. Both can be copy-pasted from documentation, without thinking. Then you need to tell Azure what runtime you want. That’s done using
runtime.txt file. In my case the content is
python-3.4 because I don’t bother with 2.x Python versions. The deployment script collects by default static files for Django. Unless you want to rewrite the deployment script completely (by creating
deploy.cmd), just create empty
.skipDjango file in root of your repository and you’re done. It probably doesn’t matter for CherryPy application anyway, but why do something more. Finally because the application uses CherryPy we add it as requirement. Putting
pip name) into
Almost there. Now just put your
*.py etc. files there and in main initialize the WSGI handler for Azure. Something like:
wsgi_app = cherrypy.Application(Hello(), '/') if __name__ == '__main__': from wsgiref.simple_server import make_server httpd = make_server('', 6600, wsgi_app) httpd.serve_forever()
The file is named
app.py because in
WSGI_ALT_VIRTUALENV_HANDLER key defining where to look for WSGI application. That’s also why the variable is named
<add key="WSGI_ALT_VIRTUALENV_HANDLER" value="app.wsgi_app" />
The part inside the
if doesn’t matter for Azure. It’s just to be able to start application from command line or something like that.
Done. Now if you wait a few seconds for deployment to happen and head to your URL you’ll the application running. On Azure Websites. No problem.
Check the cherrypy.azurewebsites.net to see the code from my repository running. You can also see the current Python and CherryPy versions (3.4.1 and 3.6.0 respectively in time of writing).
Nothing difficult really. I was just following breadcrumbs and putting together A and B. Also happy to see I can play with Python on different “places”.