diff options
| author | Yuval Adam <yuv.adm@gmail.com> | 2013-03-10 11:56:01 +0200 |
|---|---|---|
| committer | Yuval Adam <yuv.adm@gmail.com> | 2013-03-10 11:56:01 +0200 |
| commit | 7585a4509aa6df2368ae44ba0a42562f201b1937 (patch) | |
| tree | abb5ab8d12052fed6699b7eb3cefe0264b3364a7 | |
| parent | 7d578212d4c2684f8e0428ca9792d8b0b8396a5e (diff) | |
Server, websockets and client boilerplate
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Procfile.dev | 2 | ||||
| -rw-r--r-- | README.md | 24 | ||||
| -rw-r--r-- | bitrade.py | 66 | ||||
| -rw-r--r-- | index.html | 45 | ||||
| -rw-r--r-- | requirements.txt | 4 |
6 files changed, 141 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/Procfile.dev b/Procfile.dev new file mode 100644 index 0000000..8616524 --- /dev/null +++ b/Procfile.dev @@ -0,0 +1,2 @@ +redis: redis-server +tornado: python bitrade.py @@ -1,3 +1,25 @@ # Bitrade -A simple Bitcoin trading server proof-of-concept. +A <strike>highly-concurrent, fault-tolerant, malicious-user-proof,</strike> Python-based bitcoin trading server. + +Eat this, Mt.Gox - now how you gonna act? + +## Usage + +Easiest way, run all processes using [foreman](https://github.com/ddollar/foreman): + +```bash +$ foreman start -f Procfile.dev +``` + +or the Python-equivalent [honcho](https://github.com/nickstenning/honcho): + +```bash +$ honcho start -f Procfile.dev +``` + +Test client available on [http://127.0.0.1:9000](http://127.0.0.1:9000). + +## Automated Test Simulation + +WIP :) diff --git a/bitrade.py b/bitrade.py new file mode 100644 index 0000000..daf7cfd --- /dev/null +++ b/bitrade.py @@ -0,0 +1,66 @@ +# encoding: utf8 +import os + +import tornado.httpserver +import tornado.web +import tornado.websocket +import tornado.ioloop +import tornado.gen +import tornadoredis + + +c = tornadoredis.Client() +c.connect() + + +class MainHandler(tornado.web.RequestHandler): + def get(self): + self.render('index.html', title='OHAI BITRADE SERVER') + + +class OrderHandler(tornado.websocket.WebSocketHandler): + def __init__(self, *args, **kwargs): + super(OrderHandler, self).__init__(*args, **kwargs) + self.listen() + + @tornado.gen.engine + def listen(self): + self.client = tornadoredis.Client() + self.client.connect() + yield tornado.gen.Task(self.client.subscribe, 'test_channel') + self.client.listen(self.on_order) + + def on_message(self, msg): + print 'message' + c.publish('test_channel', msg) + + def on_order(self, msg): + print 'order' + if msg.kind == 'message': + self.write_message(str(msg.body)) + + def close(self): + print 'closed' + self.client.unsubscribe('test_channel') + self.client.disconnect() + + +class Application(tornado.web.Application): + def __init__(self): + handlers = [ + (r'/', MainHandler), + (r'/orders', OrderHandler), + ] + settings = dict( + debug=True, + # template_path=os.path.join(os.path.dirname(__file__), 'templates'), + # static_path=os.path.join(os.path.dirname(__file__), 'static'), + ) + tornado.web.Application.__init__(self, handlers, **settings) + +if __name__ == "__main__": + app = Application() + http_server = tornado.httpserver.HTTPServer(app) + http_server.listen(9000) + print('B⃦ Bitrade server running... (Ctrl-C to stop)') + tornado.ioloop.IOLoop.instance().start() diff --git a/index.html b/index.html new file mode 100644 index 0000000..bbc2d6a --- /dev/null +++ b/index.html @@ -0,0 +1,45 @@ +<!doctype html> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> + <title>{{ title }}</title> + <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> + <script> + $(document).ready(function() { + function show_message(msg){ + $('#messages').prepend('<p>' + msg + '<p>'); + } + + var ws = new WebSocket('ws://127.0.0.1:9000/orders'); + ws.onopen = function() { + show_message('B⃦ Connected.'); + } + ws.onmessage = function(event) { + show_message('B⃦ ' + event.data); + } + ws.onclose = function() { + show_message('B⃦ Closed.'); + } + + $('#order').click(function() { + var order = $('#order-msg').val(); + ws.send(order); + }); + }); + </script> + </head> + + <body> + + <h1>{{ title }}</h1> + <h2>Order</h2> + <p> + <input id="order-msg" value=""/> + <input id="order" type="submit" value="Send Order"/> + </p> + + <h2>Messages</h2> + <div id="messages"></div> + + </body> +</html> diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..39c6a5d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +redis==2.7.2 +tornado==2.4.1 +tornado-redis==2.4 +wsgiref==0.1.2 |
