update example
This commit is contained in:
parent
d8f5ce5675
commit
cc47d33c5e
6 changed files with 13 additions and 31 deletions
|
@ -1,8 +1,13 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
from __future__ import division
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import urllib2
|
import urllib2
|
||||||
|
import os
|
||||||
|
|
||||||
PEERLINK='http://[::1]:8842/'
|
PEERLINK = os.environ.get('PEERLINK', 'http://[::1]:8842/')
|
||||||
NAME = 'chat'
|
SERVICE = os.environ.get('PEERLINK_SERVICE', 'chat')
|
||||||
|
|
||||||
def urlopen(url, data=None, headers=None):
|
def urlopen(url, data=None, headers=None):
|
||||||
if data and not isinstance(data, str):
|
if data and not isinstance(data, str):
|
||||||
|
@ -23,12 +28,12 @@ def post(action, data=None):
|
||||||
return json.loads(urlopen(url, data))
|
return json.loads(urlopen(url, data))
|
||||||
|
|
||||||
def add(name, url):
|
def add(name, url):
|
||||||
global NAME
|
global SERVICE
|
||||||
NAME = name
|
SERVICE = name
|
||||||
return post('add', {'name': name, 'url': url})
|
return post('add', {'name': name, 'url': url})
|
||||||
|
|
||||||
def remote(peer, action, data):
|
def remote(peer, action, data):
|
||||||
url = PEERLINK + '%s/%s/%s' % (peer, NAME, action)
|
url = PEERLINK + '%s/%s/%s' % (peer, SERVICE, action)
|
||||||
if not data:
|
if not data:
|
||||||
data = {'test': True}
|
data = {'test': True}
|
||||||
print 'REMOTE', url
|
print 'REMOTE', url
|
||||||
|
|
|
@ -6,7 +6,6 @@ from __future__ import division, print_function
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import signal
|
import signal
|
||||||
import sys
|
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
|
||||||
from tornado.httpserver import HTTPServer
|
from tornado.httpserver import HTTPServer
|
||||||
|
@ -18,7 +17,6 @@ import websocket
|
||||||
from websocket import trigger_event
|
from websocket import trigger_event
|
||||||
import state
|
import state
|
||||||
from tasks import Tasks
|
from tasks import Tasks
|
||||||
from utils import json_dumps
|
|
||||||
import link
|
import link
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
@ -44,7 +42,7 @@ class BaseHandler(tornado.web.RequestHandler):
|
||||||
return
|
return
|
||||||
|
|
||||||
def render_json(self, response):
|
def render_json(self, response):
|
||||||
response = json_dumps(response)
|
response = json.dumps(response)
|
||||||
self.set_header('Content-Type', 'application/json')
|
self.set_header('Content-Type', 'application/json')
|
||||||
self.set_header('Content-Length', str(len(response)))
|
self.set_header('Content-Length', str(len(response)))
|
||||||
self.write(response)
|
self.write(response)
|
||||||
|
@ -87,11 +85,7 @@ class MainHandler(BaseHandler):
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
address = ''
|
address = ''
|
||||||
port = 8000
|
port = int(os.environ.get('PORT', 8000))
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
port = int(sys.argv[1])
|
|
||||||
link.PEERLINK=sys.argv[2]
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
websockets = []
|
websockets = []
|
||||||
tasks = None
|
tasks = None
|
||||||
info = {}
|
info = {}
|
||||||
peers = []
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
|
||||||
from __future__ import division
|
from __future__ import division
|
||||||
|
|
||||||
from Queue import Queue
|
from Queue import Queue
|
||||||
|
|
|
@ -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')
|
|
|
@ -8,7 +8,6 @@ import json
|
||||||
|
|
||||||
import state
|
import state
|
||||||
import link
|
import link
|
||||||
from utils import json_dumps
|
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger('websocket')
|
logger = logging.getLogger('websocket')
|
||||||
|
@ -40,7 +39,7 @@ class Handler(WebSocketHandler):
|
||||||
state.websockets.remove(self)
|
state.websockets.remove(self)
|
||||||
|
|
||||||
def post(self, event, data):
|
def post(self, event, data):
|
||||||
message = json_dumps([event, data])
|
message = json.dumps([event, data])
|
||||||
main = IOLoop.instance()
|
main = IOLoop.instance()
|
||||||
main.add_callback(lambda: self.write_message(message))
|
main.add_callback(lambda: self.write_message(message))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue