tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

CherryPy on Azure Websites

8 Feb 2015 4 mins Azure, Python

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.

To make the story short (longer version below) it works. And it works nicely, without any hacks. Go to my CherryPy_Azure repository and check the code. The result runs at cherrypy.azurewebsites.net.

So what do you need. First you need ptvs_virtualenv_proxy.py and 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 .deployment and 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 cherrypy (the pip name) into requirements.txt.

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 whole is app.py in my repository.

The file is named app.py because in web.config there’s WSGI_ALT_VIRTUALENV_HANDLER key defining where to look for WSGI application. That’s also why the variable is named wsgi_app.

<add key="WSGI_ALT_VIRTUALENV_HANDLER" value="app.wsgi_app" />

The part inside the __main__ 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”.

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.