def start(self): try: address = str(self.listen_address) + ':' + str(self.listen_port) (hostname, portnumber) = sockets.parse_address(address) logging.info('MIDIBand Serving on {}'.format(address)) while True: try: with sockets.PortServer(hostname, portnumber) as server: for message in server: # This messages must be re-sent to a synth later... ;) logging.info("[{}]: {}".format(hostname, message)) except TypeError: logging.warning( "A message was received with no data or a connection went down abruptly" ) except KeyError as e: logging.critical("{}".format(e)) pass except KeyboardInterrupt: logging.info("Finishing signal received")
#!/usr/bin/env python """ Serve one or more output ports. Every message received on any of the connected sockets will be sent to every output port. Example: python serve_ports.py :8080 'SH-201' 'SD-20 Part A' This simply iterates through all incoming messages. More advanced and flexible servers can be written by calling the ``accept()`` and ``accept(block=False) methods directly. See PortServer.__init__() for an example. """ import sys import mido from mido import sockets from mido.ports import MultiPort # Todo: do this with a argument parser. out = MultiPort([mido.open_output(name) for name in sys.argv[2:]]) (host, port) = sockets.parse_address(sys.argv[1]) with sockets.PortServer(host, port) as server: for message in server: print('Received {}'.format(message)) out.send(message)
def test_parse_address_normal(self, input_str, expected): assert parse_address(input_str) == expected
def test_out_of_range_port_raises_value_error(self): with pytest.raises(ValueError): parse_address(':65536')
def test_port_zero_raises_value_error(self): with pytest.raises(ValueError): parse_address(':0')
def test_non_number_port_raises_value_error(self): with pytest.raises(ValueError): parse_address(':shoe')
def test_only_colon_raises_value_error(self): with pytest.raises(ValueError): parse_address(':')
def test_empty_string_raises_value_error(self): with pytest.raises(ValueError): parse_address('')
def test_only_hostname_raises_value_error(self): with pytest.raises(ValueError): parse_address('only_hostname')
def test_too_many_colons_raises_value_error(self): with pytest.raises(ValueError): parse_address(':to_many_colons:8080')
#!/usr/bin/env python """ Simple server that just prints every message it receives. python simple_server.py localhost:8080 """ import sys import time import mido from mido import sockets from mido.ports import MultiPort if sys.argv[1:]: address = sys.argv[1] else: address = 'localhost:9080' try: (hostname, portno) = sockets.parse_address(address) print('Serving on {}'.format(address)) with sockets.PortServer(hostname, portno) as server: for message in server: print(message) except KeyboardInterrupt: pass
metavar='ADDRESS', help='host:port to serve on') arg('ports', metavar='PORT', nargs='+', help='output port to serve') return parser.parse_args() args = parse_args() try: i = 0 for name in midi_opts["output_names"]: print("{0}: {1}".format(i, name)) i += 1 print("") out = MultiPort([mido.open_output(midi_opts["output_names"][int(index)]) for index in args.ports]) (hostname, port) = sockets.parse_address(args.address) with sockets.PortServer(hostname, port) as server: for message in server: print('Received {}'.format(message)) out.send(message) except KeyboardInterrupt: pass