Serving HTTPS out of Bottle using CherryPy

I wanted to bash a couple of ideas together and see how quickly I could get something serving data from mongodb over https. I’d just started messing with python and the pymongo driver and I was (and still am) loving the quick hit python coding gives. Adding bottle as a framework made it all webby and useful feeling into the bargain :-). What is not not to like?

What I really wanted was a sweet single point solution that bottle already gave me, but with https. Past self takes over at this point….

After a bunch of digging around and reading lots of blurb, the upshot is a lightweight https server called CherryPy using bottle querying a mongodb using a regex and outputting whatever is in the first result to the screen, with a list of some of the other results, allowing the user to refine their search. The app is mickey mouse, and you do have a post where I’d prefer a redirect and using a cookie (so that browser refreshes work nicely), but the amount of data you can potentially return using the re would blow out the cookie size pretty quickly. There’s a probably a better way(tm) 😉

To do a test run you will need:

  • mongod running
  • If you want it not to throw a wobbler (I haven’t put much exception handling in it), you’ll need at least one row in testbottle.trades collection with at least one attribute of ‘Trade_Id’.
    So create a test db and record or two to play with. Start mongo shell and add some:

    use testbottle
    
    //bung one in with an easy one match re to remember
    db.trades.insert(
    	{
    		"Trade_Id" : "000457390G-1",
    		"Version_Number" : 1,
    		"Notes" : "",
    		"Settlement_Consideration" : 1721.72
    	}
    )
    
    //let us bung a small shedload in starting with x
    for (var i = 0; i < 1000; i+=1) { 
    	db.trades.insert(
    		{
    		"Trade_Id" : "x" + i, 
    		Version_Number : i, 
    		Settlement_Consideration : Math.random() * i * 100
    		}
    	)
    };
    			
    		
  • python installed
  • pip install pymongo – OR if on windows – http://pypi.python.org/pypi/pymongo use the windows msi
  • pip install bottle
  • pip install cherrypy
  • pip install pyOpenSSL – OR if on windows – http://pypi.python.org/pypi/pyOpenSSL use the windows msi
  • Create a top level folder for this project. I used pyMongoBottlePoc
  • create your own ssl cert –
    openssl req -new -x509 -keyout testserver.pem -out testserver.pem -days 365 -nodes

    Put this in your project top level folder.

  • The find_trade.py code is here. Put this in your project top level directory
  • The hello page and show found pages are here. Put these in “views” subdirectory
  • Run it by typing at the command line
    python find_trade.py
  • Use it by pointing your web browser at
    https://localhost:8082/

find_trade.py is a webserver on your localhost port 8082 which initially shows a welcome page hello_get_value.tpl asking you to input a trade id. When you click submit, find_trades.py does a regex search in the mongo database returns a cursor (trades) and the first result (trade). Bottle renders them using show_trade.tpl.

I leaned really heavily on this post from Diego XA and on the 10Gen Dev course youtube videos week 1 which have an excellent intro to python course in them. Thanks guys.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s