Python socket.SOCK_CLOEXEC() Examples

The following are code examples for showing how to use socket.SOCK_CLOEXEC(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: TFTHelper   Author: AliMZaini   File: test__socket.py    MIT License 6 votes vote down vote up
def test_connect_with_type_flags_ignored(self):
        # Issue 944
        # If we have SOCK_CLOEXEC or similar, we shouldn't be passing
        # them through to the getaddrinfo call that connect() makes
        SOCK_CLOEXEC = socket.SOCK_CLOEXEC # pylint:disable=no-member
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM | SOCK_CLOEXEC)

        def accept_once():
            conn, _ = self.listener.accept()
            fd = conn.makefile(mode='wb')
            fd.write(b'hello\n')
            fd.close()
            conn.close()

        acceptor = Thread(target=accept_once)
        s.connect(('127.0.0.1', self.port))
        fd = s.makefile(mode='rb')
        self.assertEqual(fd.readline(), b'hello\n')

        fd.close()
        s.close()

        acceptor.join() 
Example 2
Project: NiujiaoDebugger   Author: MrSrc   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def testNewAttributes(self):
        # testing .family, .type and .protocol

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.assertEqual(sock.family, socket.AF_INET)
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(sock.type,
                          (socket.SOCK_STREAM | socket.SOCK_CLOEXEC,
                           socket.SOCK_STREAM))
        else:
            self.assertEqual(sock.type, socket.SOCK_STREAM)
        self.assertEqual(sock.proto, 0)
        sock.close() 
Example 3
Project: NiujiaoDebugger   Author: MrSrc   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def test_socket_consistent_sock_type(self):
        SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
        SOCK_CLOEXEC = getattr(socket, 'SOCK_CLOEXEC', 0)
        sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC

        with socket.socket(socket.AF_INET, sock_type) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(1)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(0)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(True)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(False)
            self.assertEqual(s.type, socket.SOCK_STREAM) 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def test_unknown_socket_family_repr(self):
        # Test that when created with a family that's not one of the known
        # AF_*/SOCK_* constants, socket.family just returns the number.
        #
        # To do this we fool socket.socket into believing it already has an
        # open fd because on this path it doesn't actually verify the family and
        # type and populates the socket object.
        #
        # On Windows this trick won't work, so the test is skipped.
        fd, path = tempfile.mkstemp()
        self.addCleanup(os.unlink, path)
        unknown_family = max(socket.AddressFamily.__members__.values()) + 1

        unknown_type = max(
            kind
            for name, kind in socket.SocketKind.__members__.items()
            if name not in {'SOCK_NONBLOCK', 'SOCK_CLOEXEC'}
        ) + 1

        with socket.socket(
                family=unknown_family, type=unknown_type, proto=23,
                fileno=fd) as s:
            self.assertEqual(s.family, unknown_family)
            self.assertEqual(s.type, unknown_type)
            # some OS like macOS ignore proto
            self.assertIn(s.proto, {0, 23}) 
Example 5
Project: NiujiaoDebugger   Author: MrSrc   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def test_SOCK_CLOEXEC(self):
        with socket.socket(socket.AF_INET,
                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            self.assertFalse(s.get_inheritable()) 
Example 6
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def testNewAttributes(self):
        # testing .family, .type and .protocol

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.assertEqual(sock.family, socket.AF_INET)
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(sock.type,
                          (socket.SOCK_STREAM | socket.SOCK_CLOEXEC,
                           socket.SOCK_STREAM))
        else:
            self.assertEqual(sock.type, socket.SOCK_STREAM)
        self.assertEqual(sock.proto, 0)
        sock.close() 
Example 7
Project: Blockly-rduino-communication   Author: technologiescollege   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def test_SOCK_CLOEXEC(self):
        with socket.socket(socket.AF_INET,
                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
            self.assertTrue(s.type & socket.SOCK_CLOEXEC)
            self.assertFalse(s.get_inheritable()) 
Example 8
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_socket.py    GNU General Public License v2.0 5 votes vote down vote up
def testNewAttributes(self):
        # testing .family, .type and .protocol

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.assertEqual(sock.family, socket.AF_INET)
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(sock.type,
                          (socket.SOCK_STREAM | socket.SOCK_CLOEXEC,
                           socket.SOCK_STREAM))
        else:
            self.assertEqual(sock.type, socket.SOCK_STREAM)
        self.assertEqual(sock.proto, 0)
        sock.close() 
Example 9
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_socket.py    GNU General Public License v2.0 5 votes vote down vote up
def test_socket_consistent_sock_type(self):
        SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
        SOCK_CLOEXEC = getattr(socket, 'SOCK_CLOEXEC', 0)
        sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC

        with socket.socket(socket.AF_INET, sock_type) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(1)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(0)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(True)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(False)
            self.assertEqual(s.type, socket.SOCK_STREAM) 
Example 10
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_socket.py    GNU General Public License v2.0 5 votes vote down vote up
def test_unknown_socket_family_repr(self):
        # Test that when created with a family that's not one of the known
        # AF_*/SOCK_* constants, socket.family just returns the number.
        #
        # To do this we fool socket.socket into believing it already has an
        # open fd because on this path it doesn't actually verify the family and
        # type and populates the socket object.
        #
        # On Windows this trick won't work, so the test is skipped.
        fd, path = tempfile.mkstemp()
        self.addCleanup(os.unlink, path)
        unknown_family = max(socket.AddressFamily.__members__.values()) + 1

        unknown_type = max(
            kind
            for name, kind in socket.SocketKind.__members__.items()
            if name not in {'SOCK_NONBLOCK', 'SOCK_CLOEXEC'}
        ) + 1

        with socket.socket(
                family=unknown_family, type=unknown_type, proto=23,
                fileno=fd) as s:
            self.assertEqual(s.family, unknown_family)
            self.assertEqual(s.type, unknown_type)
            # some OS like macOS ignore proto
            self.assertIn(s.proto, {0, 23}) 
Example 11
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_socket.py    GNU General Public License v2.0 5 votes vote down vote up
def test_SOCK_CLOEXEC(self):
        with socket.socket(socket.AF_INET,
                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            self.assertFalse(s.get_inheritable()) 
Example 12
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def testNewAttributes(self):
        # testing .family, .type and .protocol

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.assertEqual(sock.family, socket.AF_INET)
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(sock.type,
                          (socket.SOCK_STREAM | socket.SOCK_CLOEXEC,
                           socket.SOCK_STREAM))
        else:
            self.assertEqual(sock.type, socket.SOCK_STREAM)
        self.assertEqual(sock.proto, 0)
        sock.close() 
Example 13
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_socket.py    GNU General Public License v3.0 5 votes vote down vote up
def test_SOCK_CLOEXEC(self):
        with socket.socket(socket.AF_INET,
                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
            self.assertTrue(s.type & socket.SOCK_CLOEXEC)
            self.assertFalse(s.get_inheritable()) 
Example 14
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_asyncore.py    GNU General Public License v3.0 5 votes vote down vote up
def test_create_socket(self):
        s = asyncore.dispatcher()
        s.create_socket(self.family)
        self.assertEqual(s.socket.family, self.family)
        SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
        sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(s.socket.type,
                          (sock_type | socket.SOCK_CLOEXEC, sock_type))
        else:
            self.assertEqual(s.socket.type, sock_type) 
Example 15
Project: cells   Author: AlesTsurko   File: test_socket.py    MIT License 5 votes vote down vote up
def testNewAttributes(self):
        # testing .family, .type and .protocol

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.assertEqual(sock.family, socket.AF_INET)
        if hasattr(socket, 'SOCK_CLOEXEC'):
            self.assertIn(sock.type,
                          (socket.SOCK_STREAM | socket.SOCK_CLOEXEC,
                           socket.SOCK_STREAM))
        else:
            self.assertEqual(sock.type, socket.SOCK_STREAM)
        self.assertEqual(sock.proto, 0)
        sock.close() 
Example 16
Project: cells   Author: AlesTsurko   File: test_socket.py    MIT License 5 votes vote down vote up
def test_socket_consistent_sock_type(self):
        SOCK_NONBLOCK = getattr(socket, 'SOCK_NONBLOCK', 0)
        SOCK_CLOEXEC = getattr(socket, 'SOCK_CLOEXEC', 0)
        sock_type = socket.SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC

        with socket.socket(socket.AF_INET, sock_type) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(1)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.settimeout(0)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(True)
            self.assertEqual(s.type, socket.SOCK_STREAM)
            s.setblocking(False)
            self.assertEqual(s.type, socket.SOCK_STREAM) 
Example 17
Project: cells   Author: AlesTsurko   File: test_socket.py    MIT License 5 votes vote down vote up
def test_unknown_socket_family_repr(self):
        # Test that when created with a family that's not one of the known
        # AF_*/SOCK_* constants, socket.family just returns the number.
        #
        # To do this we fool socket.socket into believing it already has an
        # open fd because on this path it doesn't actually verify the family and
        # type and populates the socket object.
        #
        # On Windows this trick won't work, so the test is skipped.
        fd, path = tempfile.mkstemp()
        self.addCleanup(os.unlink, path)
        unknown_family = max(socket.AddressFamily.__members__.values()) + 1

        unknown_type = max(
            kind
            for name, kind in socket.SocketKind.__members__.items()
            if name not in {'SOCK_NONBLOCK', 'SOCK_CLOEXEC'}
        ) + 1

        with socket.socket(
                family=unknown_family, type=unknown_type, proto=23,
                fileno=fd) as s:
            self.assertEqual(s.family, unknown_family)
            self.assertEqual(s.type, unknown_type)
            # some OS like macOS ignore proto
            self.assertIn(s.proto, {0, 23}) 
Example 18
Project: cells   Author: AlesTsurko   File: test_socket.py    MIT License 5 votes vote down vote up
def test_SOCK_CLOEXEC(self):
        with socket.socket(socket.AF_INET,
                           socket.SOCK_STREAM | socket.SOCK_CLOEXEC) as s:
            self.assertEqual(s.type, socket.SOCK_STREAM)
            self.assertFalse(s.get_inheritable()) 
Example 19
Project: Blockly-rduino-communication   Author: technologiescollege   File: base_events.py    GNU General Public License v3.0 4 votes vote down vote up
def _check_resolved_address(sock, address):
    # Ensure that the address is already resolved to avoid the trap of hanging
    # the entire event loop when the address requires doing a DNS lookup.
    #
    # getaddrinfo() is slow (around 10 us per call): this function should only
    # be called in debug mode
    family = sock.family

    if family == socket.AF_INET:
        host, port = address
    elif family == socket.AF_INET6:
        host, port = address[:2]
    else:
        return

    # On Windows, socket.inet_pton() is only available since Python 3.4
    if hasattr(socket, 'inet_pton'):
        # getaddrinfo() is slow and has known issue: prefer inet_pton()
        # if available
        try:
            socket.inet_pton(family, host)
        except OSError as exc:
            raise ValueError("address must be resolved (IP address), "
                             "got host %r: %s"
                             % (host, exc))
    else:
        # Use getaddrinfo(flags=AI_NUMERICHOST) to ensure that the address is
        # already resolved.
        type_mask = 0
        if hasattr(socket, 'SOCK_NONBLOCK'):
            type_mask |= socket.SOCK_NONBLOCK
        if hasattr(socket, 'SOCK_CLOEXEC'):
            type_mask |= socket.SOCK_CLOEXEC
        try:
            socket.getaddrinfo(host, port,
                               family=family,
                               type=(sock.type & ~type_mask),
                               proto=sock.proto,
                               flags=socket.AI_NUMERICHOST)
        except socket.gaierror as err:
            raise ValueError("address must be resolved (IP address), "
                             "got host %r: %s"
                             % (host, err)) 
Example 20
Project: Deribit_funding_rate_indicator   Author: Dimasik007   File: base_events.py    MIT License 4 votes vote down vote up
def _check_resolved_address(sock, address):
    # Ensure that the address is already resolved to avoid the trap of hanging
    # the entire event loop when the address requires doing a DNS lookup.
    #
    # getaddrinfo() is slow (around 10 us per call): this function should only
    # be called in debug mode
    family = sock.family

    if family == socket.AF_INET:
        host, port = address
    elif family == socket.AF_INET6:
        host, port = address[:2]
    else:
        return

    # On Windows, socket.inet_pton() is only available since Python 3.4
    if hasattr(socket, 'inet_pton'):
        # getaddrinfo() is slow and has known issue: prefer inet_pton()
        # if available
        try:
            socket.inet_pton(family, host)
        except OSError as exc:
            raise ValueError("address must be resolved (IP address), "
                             "got host %r: %s"
                             % (host, exc))
    else:
        # Use getaddrinfo(flags=AI_NUMERICHOST) to ensure that the address is
        # already resolved.
        type_mask = 0
        if hasattr(socket, 'SOCK_NONBLOCK'):
            type_mask |= socket.SOCK_NONBLOCK
        if hasattr(socket, 'SOCK_CLOEXEC'):
            type_mask |= socket.SOCK_CLOEXEC
        try:
            socket.getaddrinfo(host, port,
                               family=family,
                               type=(sock.type & ~type_mask),
                               proto=sock.proto,
                               flags=socket.AI_NUMERICHOST)
        except socket.gaierror as err:
            raise ValueError("address must be resolved (IP address), "
                             "got host %r: %s"
                             % (host, err))