Пример #1
0
 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()
Пример #2
0
 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()
Пример #3
0
 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()
Пример #4
0
 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()
Пример #5
0
 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()
Пример #6
0
 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()
Пример #7
0
 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()
Пример #8
0
 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()
Пример #9
0
 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()
Пример #10
0
 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()
Пример #11
0
 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()
Пример #12
0
    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()
Пример #13
0
 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()
Пример #14
0
 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()
Пример #15
0
 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()
Пример #16
0
 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()
Пример #17
0
 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()
Пример #18
0
 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()
Пример #19
0
    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()
Пример #20
0
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)
Пример #21
0
# 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)