def http_server_running(port, handler=default_handler): service = http.HttpServer(port, handler=handler) network.add_service(service) try: yield finally: service.stop()
def launchClient(self, name, port): if not name in self.supportedTransports.keys(): raise TransportLaunchException("Tried to launch unsupported transport %s" % name) clientClass = self.supportedTransports[name] self.handler.setTransport(clientClass) add_service(Service(self.handler.handle, port=port))
def launchServer(self, name, port): if not name in self.supportedTransports: raise TransportLaunchException('Tried to launch unsupported transport %s' % name) serverClass = self.supportedTransports[name] self.handler.setTransport(serverClass) add_service(Service(self.handler.handle, port=port))
def http_server_running(port, handler=default_handler): service = http.HttpServer(port, handler=handler, max_body_str_len=payload_multiplier * 10) network.add_service(service) try: yield finally: service.stop()
def test_lots_of_messages(): add_service(Service(handle_echo, port=8000)) try: client = Client() yield client.connect('localhost', 8000) t = time.time() for x in xrange(10000): msg = "hello, world #%s!" % x yield client.write(msg + '\r\n') echo_result = yield client.read_until("\r\n") assert echo_result.strip() == "you said: %s" % msg print '10000 loops in %.2fs' % (time.time() - t) finally: client.close()
def network_server_running(addr, port): @_o def handler(conn): while True: try: msg = yield conn.read_until(EOL) except network.ConnectionLost: break yield conn.write('you said: ' + msg.strip() + EOL) service = network.Service(handler, bindaddr=addr, port=port) network.add_service(service) try: yield finally: service.stop()
def http_server_running(port, handler=None): @_o def _handler(conn): data = 'Hello, World!' headers = http.HttpHeaders() headers.add('Content-Length', len(data)) headers.add('Content-Type', 'text/plain') headers.add('Connection', 'close') yield Return(200, headers, data) if not handler: handler = _handler service = http.HttpServer(port, handler=handler) network.add_service(service) try: yield finally: service.stop()
<script> var ws = new WebSocket('ws://localhost:8088/ws/'); ws.onopen = function(event) { console.log("Opened socket conn"); }; ws.onclose = function(event) { console.log("Closed socket conn"); }; ws.onmessage = function(event) { var str = event.data.toString(); var reversed = str.split("").reverse().join(""); $('#content').prepend(reversed); $('#realContent').append(str); }; $('#message').keyup(function() { ws.send($(this).val()); $(this).val(''); }); </script> </body> </html> """ @_o def web_handler(): yield Return(SOCKET_HTML) @_o def websocket_handler(message): yield Return(message) add_service(WebSocketServer(web_handler, websocket_handler, 8088)) eventloop.run()
import sys import time import monocle from monocle import _o monocle.init('tornado') from monocle.stack import eventloop from monocle.stack.network import add_service, Service, Client, ConnectionLost @_o def pump(input, output): while True: try: message = yield input.read(1) yield output.write(message) except ConnectionLost: output.close() break @_o def handle_socks(conn): client = Client() yield client.connect('localhost', 8050) monocle.launch(pump, conn, client) yield pump(client, conn) add_service(Service(handle_socks, port=7050)) eventloop.run_multicore(1)
import sys import logging import monocle from monocle import Return monocle.init(sys.argv[1]) from monocle.stack import eventloop from monocle.stack.network import add_service from monocle.stack.network.http import HttpHeaders, HttpServer root = logging.getLogger('') root.setLevel(logging.DEBUG) s = HttpServer(8089) @s.get("/") @s.post("/") def hello_http(req): content = "Hello, World!" headers = HttpHeaders() headers.add('Content-Type', 'text/plain') headers.add('Set-Cookie', 'test0=blar; Path=/') headers.add('Set-Cookie', 'test1=blar; Path=/') yield Return(200, headers, content) add_service(s) eventloop.run()
from monocle import _o from monocle.stack import eventloop from monocle.stack.network import add_service from monocle.stack.network.http import HttpServer @_o def hello_http(req): raise Exception("test") add_service(HttpServer(8088, handler=hello_http)) eventloop.run()
import sys import monocle from monocle import _o monocle.init(sys.argv[1]) from monocle.stack import eventloop from monocle.stack.network import add_service, Service @_o def echo(conn): their_message = yield conn.readline() yield conn.write("you said: %s\r\n" % their_message.strip()) add_service(Service(echo, 7050)) eventloop.run()
def setUp(self): self.client = http.HttpClient() self.service = http.HttpServer(helper.PORT, handler=self.handler) network.add_service(self.service)
def setUp(self): self.client = network.Client() self.service = network.Service(self.handler, bindaddr=helper.HOST, port=helper.PORT) network.add_service(self.service)
import time import monocle from monocle import _o monocle.init("tornado") from monocle.stack import eventloop from monocle.stack.network import add_service, Service, Client, ConnectionLost @_o def pump(input, output): while True: try: message = yield input.read_some() yield output.write(message) except ConnectionLost: output.close() break @_o def handle_socks(conn): client = Client() yield client.connect('localhost', 8088) monocle.launch(pump, conn, client) yield pump(client, conn) add_service(Service(handle_socks, port=8888)) eventloop.run()
import sys import monocle from monocle import _o, Return monocle.init(sys.argv[1]) from monocle.stack import eventloop from monocle.stack.network import add_service from monocle.stack.network.http import HttpHeaders, HttpServer @_o def hello_http(req): content = "Hello, World!" headers = HttpHeaders() headers.add('Content-Length', len(content)) headers.add('Content-Type', 'text/plain') headers.add('Connection', 'close') headers.add('Set-Cookie', 'test0=blar; Path=/') headers.add('Set-Cookie', 'test1=blar; Path=/') yield Return(200, headers, content) add_service(HttpServer(8088, handler=hello_http)) eventloop.run()
app = HttpServer(8888) @app.get('/') def index(req): yield Return(200, {'yes': 'indeed'}, "hello☃") @app.post('/slashable/?') def slashable(req): yield Return("slashable!") @app.get('/first/:first/second/:second_what') def params(req, first=None, second_what=None): yield Return("first: %s\nsecond: %s\n" % (first, second_what)) @app.get(re.compile('/re/[^x]')) def regexp(req): yield Return("regexp!") @app.get('/*/star/*') def stars(req): yield Return("star!") add_service(app) eventloop.run()
import sys import monocle from monocle import _o monocle.init(sys.argv[1]) from monocle.stack import eventloop from monocle.stack.network import add_service, Service @_o def lower_one(conn): raise Exception("testing") yield @_o def top_one(conn): yield lower_one(conn) add_service(Service(top_one, 12345)) eventloop.run()
app = HttpServer(8888) @app.get('/') def index(req): yield Return(200, {'yes': 'indeed'}, "hello") @app.post('/slashable/?') def slashable(req): yield Return("slashable!") @app.get('/first/:first/second/:second_what') def params(req, first=None, second_what=None): yield Return("first: %s\nsecond: %s\n" % (first, second_what)) @app.get(re.compile('/re/[^x]')) def regexp(req): yield Return("regexp!") @app.get('/*/star/*') def stars(req): yield Return("star!") add_service(app) eventloop.run()
from dust.core.util import encode from shared import * from socks import * @_o def handle_dust(conn): print('handle_dust') coder=yield handshake(conn) client = Client() yield client.connect('localhost', 9050) monocle.launch(pump, conn, client, coder.decrypt) yield pump(client, conn, coder.encrypt) @_o def handshake(conn): ekeypair=createEphemeralKeypair() epub=yield conn.read(KEY_SIZE) esession=makeEphemeralSession(ekeypair, epub).bytes print('esssion: '+encode(esession)) coder=lite_socket(esession.bytes) yield conn.write(ekeypair.public.bytes) yield Return(coder) add_service(Service(handle_dust, port=7051)) eventloop.run()
@_o def proxy(conn): client = Client() yield client.connect(backend_host, backend_port) monocle.launch(make_pumper("client"), conn, client) pumper = make_pumper("server") yield pumper(client, conn) return proxy def parse_host_port_str(host_port_str): host = "127.0.0.1" if ":" in host_port_str: host, port_str = host_port_str.split(":") else: port_str = host_port_str return host, int(port_str) if __name__ == '__main__': if len(sys.argv) < 3: print "usage: python tcp_proxy.py <frontend_port> <backend_host:port>" print "backend_host defaults to 127.0.0.1 if not specified" sys.exit(1) _, frontend_port = parse_host_port_str(sys.argv[1]) backend_host, backend_port = parse_host_port_str(sys.argv[2]) print "proxying localhost:{} -> {}:{}".format(frontend_port, backend_host, backend_port) add_service(Service(make_proxy(backend_host, backend_port), port=frontend_port)) eventloop.run()
@_o def handle_echo(conn): while True: try: message = yield conn.read_until('\r\n') except ConnectionLost: break yield conn.write("you said: %s\r\n" % message.strip()) @_o def do_echos(): try: client = Client() yield client.connect('localhost', 8000) t = time.time() for x in xrange(10000): msg = "hello, world #%s!" % x yield client.write(msg + '\r\n') echo_result = yield client.read_until("\r\n") assert echo_result.strip() == "you said: %s" % msg print('10000 loops in %.2fs' % (time.time() - t)) finally: client.close() add_service(Service(handle_echo, port=8000)) run(do_echos)
if receipt.cmd!='send': print('Unknown command: '+str(receipt.cmd)) return if receipt.args!=pub: print('Not me: '+str(receipt.args)+' '+str(pub)) return if not rsa.verify(str(receipt.sig), receipt.pub): print('Not verified') return cs.add(receipt.coin) cs.save(dir+'/coins.dat') receipts.add(receipt) receipts.save(dir+'/receipts.dat') receipt=Receive(None, pub, epoch(), receipt.coin, receipt.pub) receipt.setPrivate(priv) receipt.sign() receipts.add(receipt) receipts.save(dir+'/receipts.dat') smsg=json.dumps(receipt.save(True)) print('sending') print(smsg) yield conn.write(smsg+"\n") print('sent') add_service(Service(handle_recv, port=7050)) eventloop.run()
from socks import * @_o def handle_dust(conn): print('handle_dust') coder = yield handshake(conn) client = Client() yield client.connect('localhost', 9050) monocle.launch(pump, conn, client, coder.decrypt) yield pump(client, conn, coder.encrypt) @_o def handshake(conn): ekeypair = createEphemeralKeypair() epub = yield conn.read(KEY_SIZE) esession = makeEphemeralSession(ekeypair, epub).bytes print('esssion: ' + encode(esession)) coder = lite_socket(esession.bytes) yield conn.write(ekeypair.public.bytes) yield Return(coder) add_service(Service(handle_dust, port=7051)) eventloop.run()
import sys import monocle from monocle import _o, Return monocle.init(sys.argv[1]) from monocle.stack import eventloop from monocle.stack.network import add_service from monocle.stack.network.http import HttpHeaders, HttpServer @_o def hello_http(req): content = "Hello, World!" headers = HttpHeaders() headers.add('Content-Length', len(content)) headers.add('Content-Type', 'text/plain') headers.add('Connection', 'close') headers.add('Set-Cookie', 'test0=blar; Path=/') headers.add('Set-Cookie', 'test1=blar; Path=/') yield Return(200, headers, content) add_service(HttpServer(hello_http, 8088)) eventloop.run()
import sys import time import monocle from monocle import _o monocle.init("tornado") from monocle.stack import eventloop from monocle.stack.network import add_service, Service, Client, ConnectionLost @_o def pump(input, output): while True: try: message = yield input.read_some() yield output.write(message) except ConnectionLost: output.close() break @_o def handle_socks(conn): client = Client() yield client.connect('localhost', 8088) monocle.launch(pump, conn, client) yield pump(client, conn) add_service(Service(handle_socks, port=8888)) eventloop.run()
@_o def handle_socksDust(conn): print('connection') client = Client() yield client.connect('blanu.net', 7051) coder = yield handshake(client) monocle.launch(pump, conn, client, coder.encrypt) yield pump(client, conn, coder.decrypt) @_o def handshake(conn): ekeypair = createEphemeralKeypair() yield conn.write(ekeypair.public.bytes) epub = yield conn.read(KEY_SIZE) esession = makeEphemeralSession(ekeypair, epub) coder = lite_socket(esession) yield Return(coder) add_service(Service(handle_socksDust, port=7050)) eventloop.run()
@_o def handle_echo(conn): while True: try: message = yield conn.read_until('\r\n') message = message.decode().strip() except ConnectionLost: break yield conn.write("you said: %s\r\n" % message) @_o def do_echos(): client = Client() try: yield client.connect('localhost', 8000) t = time.time() for x in range(10000): msg = "hello, world #%s!" % x yield client.write(msg + '\r\n') echo_result = yield client.read_until('\r\n') assert echo_result.decode().strip() == "you said: %s" % msg print('10000 loops in %.2fs' % (time.time() - t)) finally: client.close() add_service(Service(handle_echo, port=8000, bindaddr="localhost")) run(do_echos)