Skip to content

Instantly share code, notes, and snippets.

/find_trade.py

Created Feb 17, 2013
Embed
What would you like to do?
Takes a partial trade id and searches a mongo database for matches
#!/usr/bin/env python
from bottle import ServerAdapter
import bottle
import sys
import pymongo
import re
import logging
# specialising for cherrypy backend using
# http://dgtool.blogspot.co.uk/2011/12/ssl.....blah in readme
# New class inherits from ServerAdapter
class MySSLCherryPy(ServerAdapter):
def run(self, handler):
from cherrypy import wsgiserver
server = wsgiserver.CherryPyWSGIServer((self.host, self.port), handler)
# If cert valeu has valid path, SSL will be used
# set it to None to disable SSL
cert = './testserver.pem'
server.ssl_certificate = cert
server.ssl_private_key = cert
try:
server.start()
finally:
server.stop()
@bottle.route('/')
def home_page():
return bottle.template('hello_get_value.tpl')
@bottle.post('/enrich_display')
def enrich_display():
//do not really need safe here as not inserting or updating
connection = pymongo.Connection("mongodb://0.0.0.0", safe=True)
db = connection.testbottle
tradeSet = db.trades
tradeId = bottle.request.forms.get("tradeId")
cond = re.compile("^" + tradeId)
trades = tradeSet.find({'Trade_Id' : cond}).limit(10)
//grab one for display
if (trades.count() > 0):
trade = trades[0]
else:
trade = None
#print (trade)
return bottle.template("show_trade.tpl", {'trade' : trade, 'trades' : trades, 'criterium' : tradeId})
@bottle.route('/testpage')
def test_page():
return "<html><title>TestPage</title><body>This is a test page</body></html>"
# playing with logging
logging.basicConfig( level=logging.DEBUG,
format='%(asctime)s | %(levelname)s | find_trade | | %(process)s | %(message)s',
filename="findtrade.log")
logging.info('Started')
# Add our new MySSLCherryPy class to the supported servers
# under the key 'mysslcherrypy'
bottle.server_names['mysslcherrypy'] = MySSLCherryPy
# reloader restarts it if you update this file
bottle.run(host='0.0.0.0', port='8082', server='mysslcherrypy', reloader=True)
logging.info('Finished')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.