def test_echo_data(self): # Echo a bunch of data and ensure it is echoed identically server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) md1 = hashlib.sha256() md2 = hashlib.sha256() def produce(): for i in range(1000): chunk = os.urandom(1024) client.write(chunk) md1.update(chunk) client.write_eof() def consume(): while True: buf = client.read1(1024) if not buf: break md2.update(buf) f1 = gruvi.spawn(produce) f2 = gruvi.spawn(consume) f1.join(); f2.join() self.assertEqual(md1.digest(), md2.digest()) server.close() client.close()
def test_read_timeout(self): server = StreamServer(echo_handler) server.listen(self.pipename()) client = StreamClient(timeout=0.01) client.connect(server.addresses[0]) self.assertRaises(Timeout, client.readline) server.close() client.close()
def test_echo_tcp_ssl(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0), **self.ssl_s_args) client = StreamClient() client.connect(server.addresses[0], **self.ssl_c_args) client.write(b'foo\n') self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_echo_tcp(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) client.write(b'foo\n') client.write_eof() self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_echo_tcp_ssl(self): server = StreamServer(echo_handler) context = self.get_ssl_context() server.listen(('127.0.0.1', 0), ssl=context) client = StreamClient() client.connect(server.addresses[0], ssl=context) client.write(b'foo\n') self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_tcp(self): server = StreamServer(echo_handler) server.listen(('localhost', 0)) addr = server.transport.getsockname() client = StreamClient() client.connect(addr) buf = b'x' * 1024 client.write(buf) result = client.read(1024) assert result == buf client.close()
def test_pipe(self): server = StreamServer(echo_handler) path = self.pipename('temp.sock') server.listen(path) client = StreamClient() client.connect(path) buf = b'x' * 1024 client.write(buf) result = client.read(1024) assert result == buf client.close()
def test_echo_pipe(self): server = StreamServer(echo_handler) server.listen(self.pipename()) client = StreamClient() client.connect(server.addresses[0]) client.write(b'foo\n') client.write_eof() self.assertEqual(client.readline(), b'foo\n') self.assertEqual(client.readline(), b'') server.close() client.close()
def test_stream(self): def echo_handler(stream, protocol, client): while True: buf = stream.read(4096) if not buf: break nbytes = stream.write(buf) server = StreamServer(echo_handler) server.listen(('localhost', 0), ssl=True, keyfile=self.certname, certfile=self.certname) addr = server.transport.getsockname() client = StreamClient() client.connect(addr, ssl=True) buf = b'x' * 1024 client.write(buf) result = b'' while len(result) != 1024: result += client.read(1024) assert result == buf client.close() server.close()
def test_echo_data(self): # Echo a bunch of data and ensure it is echoed identically server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) md1 = hashlib.sha256() md2 = hashlib.sha256() def produce(): for i in range(1000): chunk = os.urandom(1024) client.write(chunk) md1.update(chunk) client.write_eof() def consume(): while True: buf = client.read1(1024) if not buf: break md2.update(buf) f1 = gruvi.spawn(produce) f2 = gruvi.spawn(consume) f1.join() f2.join() self.assertEqual(md1.digest(), md2.digest()) server.close() client.close()
def test_connection_limit(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] server.max_connections = 10 clients = [] try: for i in range(15): client = StreamClient() client.connect(addr) client.write(b'foo\n') buf = client.readline() if buf == b'': # conneciton closed: EOF client.close() clients.append(client) except TransportError: pass self.assertLessEqual(len(server.connections), server.max_connections) for client in clients: client.close() server.close()
import logging import argparse from gruvi import get_hub, util from gruvi.stream import StreamServer logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('port', type=int) args = parser.parse_args() def echo_handler(stream, protocol, client): peer = client.getpeername() print('Connection from {0}'.format(util.saddr(peer))) while True: buf = stream.read(4096) if not buf: break stream.write(buf) print('Connection closed') server = StreamServer(echo_handler) server.listen(('0.0.0.0', args.port)) hub = get_hub() hub.switch(interrupt=True)
# Example: echo server, using StreamServer import logging import argparse from gruvi import get_hub, util from gruvi.stream import StreamServer logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('port', type=int) args = parser.parse_args() def echo_handler(stream, protocol, client): peer = client.getpeername() print('Connection from {0}'.format(util.saddr(peer))) while True: buf = stream.read(4096) if not buf: break stream.write(buf) print('Connection closed') server = StreamServer(echo_handler) server.listen(('0.0.0.0', args.port)) hub = get_hub() hub.switch(interrupt=True)