update example

This commit is contained in:
j 2014-09-01 14:29:36 +02:00
parent d8f5ce5675
commit cc47d33c5e
6 changed files with 13 additions and 31 deletions

View file

@ -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

View file

@ -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)

View file

@ -1,4 +1,3 @@
websockets = []
tasks = None
info = {}
peers = []

View file

@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
from Queue import Queue

View file

@ -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')

View file

@ -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))