示例#1
0
 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
示例#2
0
文件: network.py 项目: mopidy/mopidy
    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
示例#3
0
    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
示例#4
0
    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
示例#5
0
 def test_correctly_no_match_socket_path(self):
     assert path.get_unix_socket_path("127.0.0.1") is None
示例#6
0
 def test_correctly_matched_socket_path(self):
     assert (path.get_unix_socket_path("unix:/tmp/mopidy.socket") ==
             "/tmp/mopidy.socket")
示例#7
0
 def test_correctly_no_match_socket_path(self):
     self.assertIsNone(path.get_unix_socket_path('127.0.0.1'))
示例#8
0
 def test_correctly_matched_socket_path(self):
     self.assertEqual(
         path.get_unix_socket_path('unix:/tmp/mopidy.socket'),
         '/tmp/mopidy.socket'
     )
示例#9
0
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)