#!/usr/bin/env python #coding:utf-8 # Author : tuxpy # Email : [email protected] # Last modified : 2015-06-22 10:58:32 # Filename : run.py # Description : from gale.server import HTTPServer from app import MyApplication import sys def get_port(): if len(sys.argv) < 2: return 8000 port = sys.argv[1] if not port.isdigit(): port = 8000 return int(port) server = HTTPServer(MyApplication()) server.listen(get_port()) server.run()
# Filename : wsocket.py # Description : from __future__ import print_function from gale.websocket import WebSocketHandler from gale.web import Application, RequestHandler from gale.server import HTTPServer clients = set() class ConnHandler(WebSocketHandler): def on_open(self): print('有人连接了') def on_close(self, code, reason): print(code, reason) print('有人走了') app = Application(handlers = [ (r'/conn', ConnHandler), ], settings = {'debug': True}) @app.router(url = '/') def index(self): self.render('websocket.html') http_server = HTTPServer(app) http_server.listen(5005) http_server.run(processes = 1)
#!/usr/bin/env python #coding:utf-8 # Author : tuxpy # Email : [email protected] # Last modified : 2015-06-09 10:03:05 # Filename : helloword.py # Description : from gale.web import router, app_run """ @router(url = r'/(\w+)?', host='localhost', method = 'GET') def hello(self, name = None): self.push('hello ' + (name or 'XXX')) app_run() """ # 下面这样也可以哦 from gale.web import Application, RequestHandler from gale.server import HTTPServer class HelloHandler(RequestHandler): def GET(self, name = None): self.push('hello ' + (name or 'XXX')) app = Application(handlers = [(r'/(\w+)?', HelloHandler), ]) http_server = HTTPServer(app) http_server.listen(8080) http_server.run()