def deserialize(self, value, display=False): validators.validate_required(value, self._required) if not value.strip(): return None socket_path = path.get_unix_socket_path(value) if socket_path is not None: return 'unix:' + Path(not self._required).deserialize(socket_path) try: socket.getaddrinfo(value, None) except socket.error: raise ValueError('must be a resolveable hostname or valid IP') return value
def create_server_socket(self, host, port): socket_path = path.get_unix_socket_path(host) if socket_path is not None: # host is a path so use unix socket sock = create_unix_socket() sock.bind(socket_path) else: # ensure the port is supplied validation.check_integer(port) sock = create_tcp_socket() sock.bind((host, port)) sock.setblocking(False) sock.listen(1) return sock
def create_server_socket(self, host, port): socket_path = path.get_unix_socket_path(host) if socket_path is not None: # host is a path so use unix socket sock = create_unix_socket() sock.bind(socket_path) else: # ensure the port is supplied validation.check_integer(port) sock = create_tcp_socket() sock.bind((host, port)) sock.setblocking(False) sock.listen(1) return sock
def deserialize(self, value, display=False): value = decode(value).strip() validators.validate_required(value, self._required) if not value: return None socket_path = path.get_unix_socket_path(value) if socket_path is not None: path_str = Path(not self._required).deserialize(socket_path) return f"unix:{path_str}" try: socket.getaddrinfo(value, None) except OSError: raise ValueError("must be a resolveable hostname or valid IP") return value
def test_correctly_no_match_socket_path(self): assert path.get_unix_socket_path("127.0.0.1") is None
def test_correctly_matched_socket_path(self): assert (path.get_unix_socket_path("unix:/tmp/mopidy.socket") == "/tmp/mopidy.socket")
def test_correctly_no_match_socket_path(self): self.assertIsNone(path.get_unix_socket_path('127.0.0.1'))
def test_correctly_matched_socket_path(self): self.assertEqual( path.get_unix_socket_path('unix:/tmp/mopidy.socket'), '/tmp/mopidy.socket' )
def get_socket_address(host, port): unix_socket_path = path.get_unix_socket_path(host) if unix_socket_path is not None: return (unix_socket_path, None) else: return (host, port)