# 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 # Pre-gen, since it's static.. content = "Hello, World!" headers = http.HttpHeaders() headers.add('Content-Length', len(content)) headers.add('Content-Type', 'text/plain') def hello_http(req): return http.http_response(req, 200, headers, content) app = Application() app.add_service(Service(http.HttpServer(hello_http), 8088)) 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()
<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(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()
# 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()
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()
<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()
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()
# 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()')
# vim:ts=4:sw=4:expandtab '''Non blocking memcache get/set. ''' from diesel import Application, Service from diesel.protocols.memcache import MemCacheClient from diesel.protocols import http from diesel import log def delay_echo_server(addr): # default to 127.0.0.1:11211 m = MemCacheClient('localhost') value = m.get('mykey') values = m.get_multi(['mykey', 'mykey1', 'mykey2']) return http.Response("value from multi_get : %s and get : %s"%(str(values), str(value))) #return http.Response("value from memcache key : %s key1 : %s and key 2 : %s"%(str(value), str(value1), str(value2))) app = Application() app.add_service(Service(http.HttpServer(delay_echo_server), 8000)) 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()
yield sleep(8) yield c.sub.test.update({'name':'allrooms'}, {'name':'allrooms', 'value':['foo', 'bar', 'baz']}, upsert=1) def wait_for_doc_update(req): c = SubscribingClient(id='foo-sub') yield c.connect(BACKEND_HOST, FRONTEND_PORT) yield c.bub.foo.subscribe({'junk':'no'}) val = str((yield c.bub.foo.wait())) headers = http.HttpHeaders() headers.add('Content-Length', len(val)) headers.add('Content-Type', 'text/plain') yield http.http_response(req, 200, headers, val) def main(): c = MongoClient() yield c.connect(BACKEND_HOST, FRONTEND_PORT) yield c.drop_database('sub') yield c.drop_database('bub') print "main: dropped the db" a.add_loop(Loop(subscriber)) a.add_loop(Loop(publisher)) print "main: loops started" c.close() a = Application() a.add_service(Service(SubscriptionProxy(BACKEND_HOST, BACKEND_PORT), FRONTEND_PORT)) a.add_service(Service(http.HttpServer(wait_for_doc_update), 8088)) a.add_loop(Loop(main)) a.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()
# vim:ts=4:sw=4:expandtab '''Simple udp echo client. ''' import time from diesel import Application, UDPService, UDPLoop, send, sleep def hi_loop(): while 1: send("whatup?", addr='localhost', port=8013) print time.ctime(), "sent message to server" sleep(3) def hi_client(data, addr): print time.ctime(), "remote service said '%s'" % data app = Application() app.add_service(UDPService(hi_client, 8014)) app.add_loop(UDPLoop(hi_loop)) app.run()