def wsgiserver(handler): server = gevent.pywsgi.WSGIServer(('127.0.0.1', 54323), handler) server.start() try: yield finally: server.stop()
def exit(self, *args): """ Stop servers and exit the program. """ exceptions_ = [] for entrypoint, module in self.modules.items(): if hasattr(module, 'finalize') and callable(module.finalize): try: module.finalize(self) except Exception as err: exceptions_.append(err) for servers in list(self.servers.values()) + [self.anonymous]: for server in servers: server.stop() for filename, file in self.logfiles.items(): try: file.close() except gevent.exceptions.BlockingSwitchOutError: pass if getattr(self, 'jobs', None): try: gevent.killall(self.jobs) except gevent.exceptions.BlockingSwitchOutError: pass self.jobs = None if exceptions_: if 0 == len(exceptions_): raise exceptions_[0] else: raise exceptions.Exceptions(exceptions_)
def test_log(self, mock_datetime): now = datetime(2013, 1, 1, 2, 34, 56, 789012) t_now = int(time.mktime(now.timetuple())) mock_datetime.utcnow.side_effect = lambda: now port = random.randint(1024, 65535) received = [] def handle(socket, address): # graphite is a 4-byte length, followed by pickled representation length, = struct.unpack('!L', socket.recv(4)) d = socket.recv(length) received.append(pickle.loads(d)) server = gevent.server.StreamServer(('', port), handle) server.start() self.create({'port': port}) self.input.put(Event(metric='a.b.c', stats={'mean': 1.5, 'min': 1.0})) self.waitForEmpty() self.i.stop() self.assertEquals(0, self.input.qsize()) server.stop() self.assertEquals(1, len(received)) self.assertEquals([('a.b.c.min', (t_now, 1.0)), ('a.b.c.mean', (t_now, 1.5))], received[0])
def test_log(self, mock_datetime): now = datetime(2013, 1, 1, 2, 34, 56, 789012) t_now = int(time.mktime(now.timetuple())) mock_datetime.utcnow.side_effect = lambda: now port = random.randint(1024, 65535) received = [] def handle(socket, address): # graphite is a 4-byte length, followed by pickled representation length, = struct.unpack('!L', socket.recv(4)) d = socket.recv(length) received.append(pickle.loads(d)) server = gevent.server.StreamServer(('', port), handle) server.start() self.create({'port': port}) self.input.put(Event(metric='a.b.c', stats={'mean': 1.5, 'min': 1.0})) self.waitForEmpty() self.i.stop() self.assertEquals(0, self.input.qsize()) server.stop() self.assertEquals(1, len(received)) self.assertEquals([ ('a.b.c.min', (t_now, 1.0)), ('a.b.c.mean', (t_now, 1.5)) ], received[0])
def test_send_list(self): listing = [ np.random.rand(7, 2), 'Hello World', 1, 2.654, ] server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: Socket(x).send_list(listing)) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) sleep(0.1) data = [] list_len = client.get_int() for ind in range(list_len): data_type = client.get_string() if data_type == 'scalar': data.append(client.get_scalar()) elif data_type == 'string': data.append(client.get_string()) elif data_type == 'array': data.append(client.get_array()) utils.check_vals_in_iterable(data, listing) client.close() server.stop()
def test_send_array(self): arrays = [ np.random.rand(7, 1), np.random.rand(10, 2), np.random.rand(10, 2, 4), np.random.rand(10, 1, 3), np.random.rand(10, 4, 3, 1), ] server = SimpleServer(('127.0.0.1', 0), ) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) sleep(0.1) server_socket = Socket(server.do_read()[0]) for array in arrays: server_socket.send_array(array) data_type = client.get_string() data_len = client.get_int() shape_len = client.get_int() shape = [] for ind in range(shape_len): shape.append(client.get_int()) data_bytes = client.check_received_length(data_len) data = np.frombuffer(data_bytes, dtype=data_type) data = data.reshape(tuple(shape)) assert np.all(data == array) client.close() server.stop()
def server(handler): server = gevent.server.StreamServer(listener, handle=handler) server.start() try: yield finally: server.stop()
def test_can_reconnect(self): server = TestServer(('127.0.0.1', 0)) server.start() client_config = {'host': '127.0.0.1', 'port': server.server_port} client = Connection('test client', **client_config) client.connect() assert client.socket_connected client.reconnect() assert client.socket_connected server.stop()
def test_check_sended(self): string = 'this is a message of a given length' server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: Socket(x).check_sended(string.encode())) server.start() client = socket.create_connection(('127.0.0.1', server.server_port)) response = client.recv(4096) assert response == b'this is a message of a given length' client.close() server.stop()
def test_get_string(self): string = 'this is a message' server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: Socket(x).send_string(string)) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) assert client.get_string() == string client.close() server.stop()
def server(handler, backlog=1): server = gevent.server.StreamServer(listener, backlog=backlog, handle=handler, keyfile=KEY, certfile=CERT) server.start() try: yield finally: server.stop()
def test_get_int(self): one_integer = 15489 server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: x.sendall(Socket.int_to_bytes(one_integer))) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) assert client.get_int() == one_integer client.close() server.stop()
def test_can_make_connection(self): server = TestServer(('127.0.0.1', 0)) server.start() client_config = {'host': '127.0.0.1', 'port': server.server_port} client = Connection('test client', **client_config) client.connect() assert client.socket_connected response = client.input_queue.get() assert response == 'testing the connection' assert not client.needs_reconnect server.stop()
def test_can_send_data(self): server = TestServer(('127.0.0.1', 0)) server.start() client_config = {'host': '127.0.0.1', 'port': server.server_port} client = Connection('test client', **client_config) client.connect() assert client.socket_connected client.output_queue.put('testing sending') time.sleep(2) self.assertEqual(server.input_data, 'testing sending\r\n') server.stop()
def server(handler, backlog=1): server = gevent.server.StreamServer(("localhost", 0), backlog=backlog, handle=handler, keyfile=KEY, certfile=CERT) server.start() try: yield (server.server_host, server.server_port) finally: server.stop()
def test_eventual_connect(): class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall("hello and goodbye!") socket.shutdown(0) server = SimpleServer(('127.0.0.1', 16667)) gevent.spawn_later(0.5, server.start) client = util.connect_and_retry(('127.0.0.1', 16667), max_delay=1) lines = [line for line in util.line_protocol(client)] assert len(lines) == 1, "Didn't receive the line" server.stop()
def test_does_connect(): class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall("hello and goodbye!") socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = util.connect_and_retry(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == 1, "Didn't receive the line" server.stop()
def test_check_received_length(self): string = 'this is a message' server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: Socket(x).check_sended(string.encode())) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) assert client.check_received_length(len( string.encode())) == string.encode() client.close() server.stop()
def server(handler, backlog=1): server = gevent.server.StreamServer( ("localhost", 0), backlog=backlog, handle=handler, keyfile=KEY, certfile=CERT) server.start() try: yield (server.server_host, server.server_port) finally: server.stop()
def server(handler, backlog=1): server = gevent.server.StreamServer( listener, backlog=backlog, handle=handler, keyfile=KEY, certfile=CERT) server.start() try: yield finally: server.stop()
def test_does_connect(): class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall("hello and goodbye!") socket.shutdown(0) server = SimpleServer(("127.0.0.1", 0)) server.start() client = util.connect_and_retry(("127.0.0.1", server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == 1, "Didn't receive the line" server.stop()
def test_eventual_connect(): class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall("hello and goodbye!") socket.shutdown(0) server = SimpleServer(("127.0.0.1", 16667)) gevent.spawn_later(0.5, server.start) client = util.connect_and_retry(("127.0.0.1", 16667), max_delay=1) lines = [line for line in util.line_protocol(client)] assert len(lines) == 1, "Didn't receive the line" server.stop()
def test_send_string(self): string = 'this is a message' server = SimpleServer( ('127.0.0.1', 0), handle_fun=lambda x: Socket(x).send_string(string)) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) len_string = int.from_bytes(client.check_received_length(4), 'big') assert len_string == len(string) assert client.check_received_length(len_string) == string.encode() client.close() server.stop()
def test_get_scalar(self): scalars = [15489, 2.4589] server = SimpleServer(('127.0.0.1', 0), ) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) sleep(0.1) server_socket = Socket(server.do_read()[0]) for scalar in scalars: server_socket.send_scalar(scalar) assert client.get_scalar() == scalar client.close() server.stop()
def test_one_liner(): one_line = 'hello and goodbye!' class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall(one_line) socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == 1, "Got too many (or not enough) lines" assert lines[0] == one_line, "Didn't get the line expected" server.stop()
def test_multi_lines_rn(): one_line = 'hello and goodbye!' number_lines = 5 class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall('\r\n'.join([one_line for n in xrange(number_lines)])) socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == number_lines, "Got too many (or not enough) lines" assert lines.pop() == one_line, "Didn't get the line expected" server.stop()
def test_strip_on_lines(): one_line = 'hello and goodbye!\n' number_lines = 5 class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall(''.join([one_line for n in xrange(number_lines)])) socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == number_lines, "Got too many (or not enough) lines" assert lines.pop() != one_line, "Line includes newlines (or something)" assert lines.pop() == one_line.strip(), "Line doesn't match when stripped" server.stop()
def test_multi_lines(): one_line = 'hello and goodbye!' number_lines = 5 class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall('\n'.join([one_line for n in xrange(number_lines)])) socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client)] assert len(lines) == number_lines, "Got too many (or not enough) lines" assert lines.pop() == one_line, "Didn't get the line expected" server.stop()
def test_no_strip_on_lines(): one_line = 'hello and goodbye!\n' number_lines = 5 class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall(''.join([one_line for n in xrange(number_lines)])) socket.sendall('\n') socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client, strip=False)] assert len(lines) == number_lines+1, "Got too many (or not enough) lines" assert lines.pop() == '\n', "Didn't get empty line" assert lines.pop() == one_line, "Line doesn't match line with newline" server.stop()
def main(number=1, bind=DEFAULT_BIND, port=DEFAULT_PORT, **kwargs): servers = [] logging.info('starting simulator...') for i in range(number): address = bind, port+i server = PSSimulator(address) server.start() servers.append(server) server.log.info('simulator listenning on %r!', address) try: while True: # gevent.joinall(servers) gevent.sleep(1) except KeyboardInterrupt: logging.info('Ctrl-C pressed. Bailing out!') for server in servers: server.stop()
def test_send_another_list(self): listing = [ 'another list that should raise an exception because there is a boolean that is not a valid type', [ 'gg', ], ] server = SimpleServer(('127.0.0.1', 0), ) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) sleep(0.1) server_socket = Socket(server.do_read()[0]) with pytest.raises(TypeError): assert server_socket.send_list(listing) server.stop()
def test_no_strip_on_lines(): one_line = 'hello and goodbye!\n' number_lines = 5 class SimpleServer(gevent.server.StreamServer): def handle(self, socket, address): socket.sendall(''.join([one_line for n in xrange(number_lines)])) socket.sendall('\n') socket.shutdown(0) server = SimpleServer(('127.0.0.1', 0)) server.start() client = gevent.socket.create_connection(('127.0.0.1', server.server_port)) lines = [line for line in util.line_protocol(client, strip=False)] assert len(lines) == number_lines + 1, "Got too many (or not enough) lines" assert lines.pop() == '\n', "Didn't get empty line" assert lines.pop() == one_line, "Line doesn't match line with newline" server.stop()
def test_send_scalar(self): scalars = [15489, 2.4589] server = SimpleServer(('127.0.0.1', 0), ) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) sleep(0.1) server_socket = Socket(server.do_read()[0]) for scalar in scalars: server_socket.send_scalar(scalar) data_type = client.get_string() data_len = client.get_int() data_bytes = client.check_received_length(data_len) data = np.frombuffer(data_bytes, dtype=data_type)[0] assert data == scalar client.close() server.stop()
def test_get_array(self): arrays = [ np.random.rand(7, 1), np.random.rand(10, 2), np.random.rand(10, 2, 4), np.random.rand(10, 1, 3), np.random.rand(10, 4, 3, 1), ] server = SimpleServer(('127.0.0.1', 0), ) server.start() client = Socket( socket.create_connection(('127.0.0.1', server.server_port))) server_socket = Socket(server.do_read()[0]) for array in arrays: server_socket.send_array(array) data = client.get_array() assert np.all(data == array) client.close() server.stop()
def test_unavailable(self): port = random.randint(1024, 65535) received = [] def handle(socket, address): # graphite is a 4-byte length, followed by pickled representation length, = struct.unpack('!L', socket.recv(4)) d = socket.recv(length) received.append(pickle.loads(d)) server = gevent.server.StreamServer(('', port), handle) self.create({'port': port}) self.input.put(Event(metric='a.b.c', stats={'mean': 1.5, 'min': 1.0})) server.start() self.waitForEmpty() self.assertEquals(0, self.input.qsize()) self.i.stop() server.stop() self.assertEquals(1, len(received))
def stop(self): for server, task in self.servers: server.stop() self.servers = []
def die(server): """ Kill the server """ server.stop()
gevent.wait([gevent.spawn(send_json, plr.client, msg) for plr in players]) def send_json(client, obj): data = json.dumps(obj).encode() client.sendall(len(data).to_bytes(4, 'big')) client.sendall(data) def recv_json(client): size = b''.join(client.recv(1) for _ in range(4)) size = int.from_bytes(size, 'big') if not size: raise ConnectionResetError buffer = b'' while len(buffer) < size: buffer += client.recv(size - len(buffer)) print(f'read {buffer}') return json.loads(buffer.decode()) ADDRESS = ('127.0.0.1', 7575) if __name__ == '__main__': server = gevent.server.StreamServer(ADDRESS, client_connected) server.start() print(f'server started {server.server_host}:{server.server_port}') try: server.serve_forever() except KeyboardInterrupt: server.stop()
def test_methods(self, qtbot): server = SimpleServer(('127.0.0.1', 6341), ) server.start() params = [{ 'title': 'Device index:', 'name': 'device', 'type': 'int', 'value': 0, 'max': 3, 'min': 0 }, { 'title': 'Infos:', 'name': 'infos', 'type': 'str', 'value': "one_info", 'readonly': True }, { 'title': 'Line Settings:', 'name': 'line_settings', 'type': 'group', 'expanded': False, 'children': [ { 'name': 'someparam', 'type': 'float', 'value': 15.54, 'readonly': True }, ] }] param = Parameter.create(name='settings', type='group', children=params) client = TCPClient(ipaddress="127.0.0.1", port=6341, params_state=param.saveState(), client_type="sometype") client.cmd_signal.connect(self.get_cmd_signal) #check init method assert client.ipaddress == "127.0.0.1" assert client.port == 6341 assert client.client_type == 'sometype' client.socket = Socket( native_socket.socket(socket.AF_INET, socket.SOCK_STREAM)) client.socket.connect((client.ipaddress, client.port)) sleep(0.5) server_socket = Socket(server.do_read()[0]) client.send_data([np.array([0, 1, 2, 3]), 'item', 5.1]) assert server_socket.get_string() == 'Done' utils.check_vals_in_iterable(server_socket.get_list(), [np.array([0, 1, 2, 3]), 'item', 5.1]) client.send_infos_xml(custom_tree.parameter_to_xml_string(param)) assert server_socket.get_string() == 'Infos' assert server_socket.get_string( ) == custom_tree.parameter_to_xml_string(param).decode() client.send_info_string('someinfo', 'this is an info') assert server_socket.get_string() == 'Info' assert server_socket.get_string() == 'someinfo' assert server_socket.get_string() == 'this is an info' #test queue_command client.cmd_signal.connect(self.get_cmd_signal) client_manager = ClientObjectManager() client_manager.cmd_signal.connect(client.queue_command) with pytest.raises(Exception): client.queue_command(utils.ThreadCommand('Weird command')) #test get_data server_socket.send_string('set_info') server_socket.send_list(['line_settings', 'someparam']) server_socket.send_string( custom_tree.parameter_to_xml_string( param.child('line_settings', 'someparam'))) msg = client.socket.get_string() client.get_data(msg) assert self.command == 'set_info' utils.check_vals_in_iterable( self.attributes, [['line_settings', 'someparam'], custom_tree.parameter_to_xml_string( param.child('line_settings', 'someparam')).decode()]) server_socket.send_string('move_abs') server_socket.send_scalar(12.546) msg = client.socket.get_string() client.get_data(msg) assert self.command == 'move_abs' utils.check_vals_in_iterable(self.attributes, [12.546]) server_socket.send_string('move_rel') server_socket.send_scalar(3.2) msg = client.socket.get_string() client.get_data(msg) assert self.command == 'move_rel' utils.check_vals_in_iterable(self.attributes, [3.2]) server.stop()