def main(argv=None): try: configuration = Configuration(argv) configuration.app = app = Application() website = Website(configuration) configuration.website = website # to support re-handling, especially website = configuration.hooks.run('startup', website) # change current working directory os.chdir(configuration.root) if configuration.conf.aspen.no('changes_kill'): # restart for template files too; # TODO can't we just invalidate the simplate cache for these? dot_aspen = join(configuration.root, '.aspen') for root, dirs, files in os.walk(dot_aspen): for filename in files: restarter.add(join(root, filename)) app.add_loop(Loop(restarter.loop)) port = configuration.address[1] app.add_service(Service(http.HttpServer(website), port)) log.warn("Greetings, program! Welcome to port %d." % port) app.run() except KeyboardInterrupt, SystemExit: configuration.hooks.run('shutdown', website)
def test_basic_echo(self): PORT = 51372 NUM = 5 app, touch, acc = self.prepare_test() def handle_echo(conn): said = until_eol() send('YS:' + said) touch() def b_client(): time.sleep(0.5) r = uuid4().hex s = socket.socket() s.connect(('localhost', PORT)) s.sendall(r + '\r\n') back = s.recv(65536) cl = s.recv(500) acc[r] = back, cl for x in xrange(NUM): thread.start_new_thread(b_client, ()) app.add_service(Service(handle_echo, PORT)) self.run_test(NUM) for key, (back, cl) in acc.iteritems(): assert back == 'YS:%s\r\n' % key assert cl == ''
def __init__(self, wsgi_callable, port=80, iface=''): Application.__init__(self) self.port = port self.wsgi_callable = wsgi_callable http_service = Service( HttpServer(WSGIRequestHandler(wsgi_callable, port)), port, iface) self.add_service(http_service)
def test_byte_boundaries(self): PORT = 51372 NUM = 5 app, touch, acc = self.prepare_test() def handle_echo(conn): size = int((until('|'))[:-1]) said = receive(size) out = 'YS:' + said send(out) touch() def b_client(): time.sleep(0.5) r = uuid4().bytes s = socket.socket() s.connect(('localhost', PORT)) s.send('%s|' % len(r)) s.sendall(r) time.sleep(0.2) back = s.recv(65536) cl = s.recv(500) acc[r] = back, cl for x in xrange(NUM): thread.start_new_thread(b_client, ()) app.add_service(Service(handle_echo, PORT)) self.run_test(NUM) for key, (back, cl) in acc.iteritems(): assert back == 'YS:%s' % key assert cl == ''
def handle_echo(remote_addr): while True: message = until('\r\n') send("you said: %s" % message) class EchoClient(Client): @call def echo(self, message): send(message + '\r\n') back = until("\r\n") return back log = log.name('echo-system') def do_echos(): with EchoClient('localhost', 8000, ssl_ctx=SSL.Context(SSL.TLSv1_METHOD)) as client: t = time.time() for x in xrange(5000): msg = "hello, world #%s!" % x echo_result = client.echo(msg) assert echo_result.strip() == "you said: %s" % msg log.info('5000 loops in {0:.2f}s', time.time() - t) quickstop() quickstart(Service(handle_echo, port=8000, ssl_ctx=server_ctx), do_echos)
def run_server(me, cluster): init_group(cluster) port = int(me.split(':')[-1]) return [locker, group, Service(handle_client, port)]
# vim:ts=4:sw=4:expandtab '''Demonstrate sleep-type behavior server-side. ''' from diesel import Application, Service, until_eol, sleep, send def delay_echo_server(addr): inp = until_eol() for x in xrange(4): sleep(2) send(str(x) + '\r\n') send("you said %s" % inp) app = Application() app.add_service(Service(delay_echo_server, 8013)) app.run()
def bind_and_listen(self): Service.bind_and_listen(self) me.id = '%s/%s' % (os.uname()[1], self.port)
# vim:ts=4:sw=4:expandtab '''Simple echo server. ''' from diesel import Application, Service, until_eol, send def hi_server(addr): while 1: inp = until_eol() if inp.strip() == "quit": break send("you said %s" % inp) app = Application() app.add_service(Service(hi_server, 8013)) app.run()
# vim:ts=4:sw=4:expandtab '''The oh-so-canonical "Hello, World!" http server. ''' from diesel import Application, Service from diesel.protocols import http def hello_http(req): return http.Response("Hello, World!") app = Application() app.add_service(Service(http.HttpServer(hello_http), 8088)) import cProfile cProfile.run('app.run()')
msg = thread(self.read_chat_message, "").strip() self.input.put(msg) @call def chat(self): fork(self.input_handler) nick = self.input.get() send("%s\r\n" % nick) while True: evt, data = first(until_eol=True, waits=[self.input]) if evt == "until_eol": print data.strip() else: send("%s\r\n" % data) def chat_client(): with ChatClient('localhost', 8000) as c: c.chat() app = Application() if sys.argv[1] == "server": app.add_service(Service(chat_server, 8000)) elif sys.argv[1] == "client": app.add_loop(Loop(chat_client)) else: print "USAGE: python %s [server|client]" % sys.argv[0] raise SystemExit(1) app.run()
message = until('\r\n') send("you said: %s" % message) class EchoClient(Client): @call def echo(self, message): send(message + '\r\n') back = until("\r\n") return back app = Application() log = log.sublog('echo-system', log.info) def do_echos(): client = EchoClient('localhost', 8000, ssl_ctx=SSL.Context(SSL.TLSv1_METHOD)) t = time.time() for x in xrange(5000): msg = "hello, world #%s!" % x echo_result = client.echo(msg) assert echo_result.strip() == "you said: %s" % msg log.info('5000 loops in %.2fs' % (time.time() - t)) app.halt() server_ctx = SSL.Context(SSL.TLSv1_METHOD) server_ctx.use_privatekey_file('snakeoil-key.pem') server_ctx.use_certificate_file('snakeoil-cert.pem') app.add_service(Service(handle_echo, port=8000, ssl_ctx=server_ctx)) app.add_loop(Loop(do_echos)) app.run()
from diesel import Application, Service, Client, Loop, until, call, response def handle_echo(remote_addr): while True: message = yield until('\r\n') yield "you said: %s" % message class EchoClient(Client): @call def echo(self, message): yield message + '\r\n' back = yield until("\r\n") yield response(back) app = Application() def do_echos(): client = EchoClient() yield client.connect('localhost', 8000) t = time.time() for x in xrange(5000): msg = "hello, world #%s!" % x echo_result = yield client.echo(msg) assert echo_result.strip() == "you said: %s" % msg print '5000 loops in %.2fs' % (time.time() - t) app.halt() app.add_service(Service(handle_echo, port=8000)) app.add_loop(Loop(do_echos)) app.run()
def handle_echo(remote_addr): while True: message = until('\r\n') send("you said: %s" % message) class EchoClient(Client): @call def echo(self, message): send(message + '\r\n') back = until("\r\n") return back log = log.name('echo-system') def do_echos(): client = EchoClient('localhost', 8000) t = time.time() for x in xrange(5000): msg = "hello, world #%s!" % x echo_result = client.echo(msg) assert echo_result.strip() == "you said: %s" % msg log.info('5000 loops in {0:.2f}s', time.time() - t) quickstop() quickstart(Service(handle_echo, port=8000), do_echos)
def __init__(self): Service.__init__(self, handle_conn, 0)
<input type="text" size="40" id="the-i" /> <input type="button" value="Update Message" onclick="push(); return false" /> </body> </html> ''' % LOCATION def web_handler(req): heads = HttpHeaders() heads.add('Content-Length', len(content)) heads.add('Content-Type', 'text/html') return http_response(req, 200, heads, content) import time def socket_handler(req, inq, outq): message = "hello, there!" while True: try: v = inq.get(timeout=0.5) except QueueTimeout: pass else: message = v['message'] outq.put(WSD(message=message, time=time.time())) a = Application() a.add_service(Service(WebSocketServer(web_handler, socket_handler, LOCATION), 8091)) a.run()
if line == "": break parts = line.split() cmd = parts[0] if cmd == "get": key = parts[1] try: val = CACHE[key] send("VALUE %s 0 %d\r\n" % (key, len(val))) send(val + "\r\n") except KeyError: pass send("END\r\n") elif cmd == "set": key = parts[1] #exp = parts[2] #flags = parts[3] length = int(parts[4]) val = receive(length + 2)[:length] CACHE[key] = val send("STORED\r\n") if __name__ == "__main__": quickstart(Service(handle_con, 11211))