From cc47d33c5e2e7cb62d90ec72d5d95cb8ce83985b Mon Sep 17 00:00:00 2001 From: j Date: Mon, 1 Sep 2014 14:29:36 +0200 Subject: [PATCH] update example --- example/link.py | 15 ++++++++++----- example/server.py | 10 ++-------- example/state.py | 1 - example/tasks.py | 1 - example/utils.py | 14 -------------- example/websocket.py | 3 +-- 6 files changed, 13 insertions(+), 31 deletions(-) delete mode 100644 example/utils.py diff --git a/example/link.py b/example/link.py index 38337c5..f4f24c8 100644 --- a/example/link.py +++ b/example/link.py @@ -1,8 +1,13 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division + import json import urllib2 +import os -PEERLINK='http://[::1]:8842/' -NAME = 'chat' +PEERLINK = os.environ.get('PEERLINK', 'http://[::1]:8842/') +SERVICE = os.environ.get('PEERLINK_SERVICE', 'chat') def urlopen(url, data=None, headers=None): if data and not isinstance(data, str): @@ -23,12 +28,12 @@ def post(action, data=None): return json.loads(urlopen(url, data)) def add(name, url): - global NAME - NAME = name + global SERVICE + SERVICE = name return post('add', {'name': name, 'url': url}) def remote(peer, action, data): - url = PEERLINK + '%s/%s/%s' % (peer, NAME, action) + url = PEERLINK + '%s/%s/%s' % (peer, SERVICE, action) if not data: data = {'test': True} print 'REMOTE', url diff --git a/example/server.py b/example/server.py index ac3ec43..cab7430 100755 --- a/example/server.py +++ b/example/server.py @@ -6,7 +6,6 @@ from __future__ import division, print_function import json import os import signal -import sys import mimetypes from tornado.httpserver import HTTPServer @@ -18,7 +17,6 @@ import websocket from websocket import trigger_event import state from tasks import Tasks -from utils import json_dumps import link import logging @@ -44,7 +42,7 @@ class BaseHandler(tornado.web.RequestHandler): return def render_json(self, response): - response = json_dumps(response) + response = json.dumps(response) self.set_header('Content-Type', 'application/json') self.set_header('Content-Length', str(len(response))) self.write(response) @@ -87,11 +85,7 @@ class MainHandler(BaseHandler): if __name__ == '__main__': address = '' - port = 8000 - - if len(sys.argv) > 1: - port = int(sys.argv[1]) - link.PEERLINK=sys.argv[2] + port = int(os.environ.get('PORT', 8000)) logging.basicConfig(level=logging.DEBUG) diff --git a/example/state.py b/example/state.py index 66e672c..867b240 100644 --- a/example/state.py +++ b/example/state.py @@ -1,4 +1,3 @@ websockets = [] tasks = None info = {} -peers = [] diff --git a/example/tasks.py b/example/tasks.py index 377735c..28e649b 100644 --- a/example/tasks.py +++ b/example/tasks.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 - from __future__ import division from Queue import Queue diff --git a/example/utils.py b/example/utils.py deleted file mode 100644 index 060fa74..0000000 --- a/example/utils.py +++ /dev/null @@ -1,14 +0,0 @@ -import json -import datetime - -def _to_json(python_object): - if isinstance(python_object, datetime.datetime): - if python_object.year < 1900: - tt = python_object.timetuple() - return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6]) - return python_object.strftime('%Y-%m-%dT%H:%M:%SZ') - raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object))) - -def json_dumps(obj): - indent = 2 - return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode('utf-8') diff --git a/example/websocket.py b/example/websocket.py index 99406f8..748595a 100644 --- a/example/websocket.py +++ b/example/websocket.py @@ -8,7 +8,6 @@ import json import state import link -from utils import json_dumps import logging logger = logging.getLogger('websocket') @@ -40,7 +39,7 @@ class Handler(WebSocketHandler): state.websockets.remove(self) def post(self, event, data): - message = json_dumps([event, data]) + message = json.dumps([event, data]) main = IOLoop.instance() main.add_callback(lambda: self.write_message(message))