Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_port_zero_raises_value_error(self):
with pytest.raises(ValueError):
parse_address(':0')
def test_only_colon_raises_value_error(self):
with pytest.raises(ValueError):
parse_address(':')
def test_non_number_port_raises_value_error(self):
with pytest.raises(ValueError):
parse_address(':shoe')
def test_parse_address(self):
from mido.sockets import parse_address
self.assertTrue(('', 8080) == parse_address(':8080'))
self.assertTrue(('localhost', 8080) == parse_address('localhost:8080'))
self.assertRaises(ValueError, parse_address, ':to_many_colons:8080')
self.assertRaises(ValueError, parse_address, 'only_hostname')
self.assertRaises(ValueError, parse_address, '')
self.assertRaises(ValueError, parse_address, ':')
self.assertRaises(ValueError, parse_address, ':shoe')
self.assertRaises(ValueError, parse_address, ':0')
self.assertRaises(ValueError, parse_address, ':65536') # Out of range.
def test_parse_address(self):
from mido.sockets import parse_address
self.assertTrue(('', 8080) == parse_address(':8080'))
self.assertTrue(('localhost', 8080) == parse_address('localhost:8080'))
self.assertRaises(ValueError, parse_address, ':to_many_colons:8080')
self.assertRaises(ValueError, parse_address, 'only_hostname')
self.assertRaises(ValueError, parse_address, '')
self.assertRaises(ValueError, parse_address, ':')
self.assertRaises(ValueError, parse_address, ':shoe')
self.assertRaises(ValueError, parse_address, ':0')
self.assertRaises(ValueError, parse_address, ':65536') # Out of range.
#!/usr/bin/env python
"""
Forward all messages from one or more ports to server.
Example:
python forward_ports.py localhost:8080 'Keyboard MIDI 1'
"""
import sys
import mido
host, port = mido.sockets.parse_address(sys.argv[1])
ports = [mido.open_input(name) for name in sys.argv[2:]]
with mido.sockets.connect(host, port) as server_port:
print('Connected.')
for message in mido.ports.multi_receive(ports):
print('Sending {}'.format(message))
server_port.send(message)
Example:
python simple_client.py localhost:8080
"""
import sys
import time
import random
import mido
if sys.argv[1:]:
address = sys.argv[1]
else:
address = 'localhost:9080'
host, port = mido.sockets.parse_address(address)
notes = [60, 67, 72, 79, 84, 79, 72, 67, 60]
on = mido.Message('note_on', velocity=100)
off = mido.Message('note_off', velocity=100)
base = random.randrange(12)
print('Connecting to {}'.format(address))
with mido.sockets.connect(host, port) as server_port:
try:
message = mido.Message('program_change')
for note in notes:
on.note = off.note = base + note
server_port.send(on)
time.sleep(0.05)
#!/usr/bin/env python
"""
Simple server that just prints every message it receives.
python simple_server.py localhost:8080
"""
import sys
from mido import sockets
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