当前位置: 首页>>代码示例>>Python>>正文


Python socket.error方法代码示例

本文整理汇总了Python中socket.socket.error方法的典型用法代码示例。如果您正苦于以下问题:Python socket.error方法的具体用法?Python socket.error怎么用?Python socket.error使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在socket.socket的用法示例。


在下文中一共展示了socket.error方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _validate_request

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def _validate_request(self, proc=None):
        '''Validates request and simulates errors when not valid'''

        # Password not part of URL if connecting via socket
        transport_password = self.transport.password if self.transport else ""

        # `host` is optional connecting via socket
        transport_socket = self.transport.serverurl if self.transport else ""

        # if 'socket_file' in self.url:
        if 'invalid_host' in self.url or 'invalid_socket' in transport_socket:
            # Simulate connecting to an invalid host/port in order to
            # raise `socket.error: [Errno 111] Connection refused`
            socket().connect(('localhost', 38837))
        elif 'invalid_pass' in self.url or 'invalid_pass' in transport_password:
            # Simulate xmlrpc exception for invalid credentials
            raise xmlrpclib.ProtocolError(self.url[7:], 401, 'Unauthorized', None)
        elif proc is not None and 'invalid' in proc:
            # Simulate xmlrpc exception for process not found
            raise xmlrpclib.Fault(10, 'BAD_NAME') 
开发者ID:DataDog,项目名称:integrations-core,代码行数:22,代码来源:test_supervisord_unit.py

示例2: test_flags

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_flags(self):
        """
        The C{flags} argument to L{send1msg} is passed on to the underlying
        C{sendmsg} call, to affect it in whatever way is defined by those
        flags.
        """
        # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
        # makes the send a non-blocking call, even if the socket is in blocking
        # mode.  See also test_flags in RecvmsgTests
        for i in range(1024):
            try:
                send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT)
            except error as e:
                self.assertEqual(e.args[0], errno.EAGAIN)
                break
        else:
            self.fail(
                "Failed to fill up the send buffer, "
                "or maybe send1msg blocked for a while") 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:21,代码来源:test_sendmsg.py

示例3: test_flags

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_flags(self):
        """
        The C{flags} argument to L{send1msg} is passed on to the underlying
        C{sendmsg} call, to affect it in whatever way is defined by those
        flags.
        """
        # Just exercise one flag with simple, well-known behavior. MSG_DONTWAIT
        # makes the send a non-blocking call, even if the socket is in blocking
        # mode.  See also test_flags in RecvmsgTests
        for i in range(8 * 1024):
            try:
                send1msg(self.input.fileno(), "x" * 1024, MSG_DONTWAIT)
            except error as e:
                self.assertEqual(e.args[0], errno.EAGAIN)
                break
        else:
            self.fail(
                "Failed to fill up the send buffer, "
                "or maybe send1msg blocked for a while") 
开发者ID:wistbean,项目名称:learn_python3_spider,代码行数:21,代码来源:test_sendmsg.py

示例4: reserve

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def reserve(ip=LOCALHOST, port=0):
    """Bind to an ephemeral port, force it into the TIME_WAIT state, and unbind it.

    This means that further ephemeral port alloctions won't pick this "reserved" port,
    but subprocesses can still bind to it explicitly, given that they use SO_REUSEADDR.
    By default on linux you have a grace period of 60 seconds to reuse this port.
    To check your own particular value:
    $ cat /proc/sys/net/ipv4/tcp_fin_timeout
    60

    By default, the port will be reserved for localhost (aka 127.0.0.1).
    To reserve a port for a different ip, provide the ip as the first argument.
    Note that IP 0.0.0.0 is interpreted as localhost.
    """
    port = int(port)
    with contextlib.closing(socket()) as s:
        s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        try:
            s.bind((ip, port))
        except SocketError as e:
            # socket.error: EADDRINUSE Address already in use
            if e.errno == errno.EADDRINUSE and port != 0:
                s.bind((ip, 0))
            else:
                raise

        # the connect below deadlocks on kernel >= 4.4.0 unless this arg is greater than zero
        s.listen(1)

        sockname = s.getsockname()

        # these three are necessary just to get the port into a TIME_WAIT state
        with contextlib.closing(socket()) as s2:
            s2.connect(sockname)
            s.accept()
            return sockname[1] 
开发者ID:Yelp,项目名称:ephemeral-port-reserve,代码行数:38,代码来源:ephemeral_port_reserve.py

示例5: read_ha_proxy_stats

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def read_ha_proxy_stats(haproxy_stats_socket):
    conn = socket(AF_UNIX, SOCK_STREAM)
    try:
        conn.connect(haproxy_stats_socket)
        conn.sendall(b'show stat\r\n')
        data = conn.recv(BUFFER_SIZE)
        while len(data) % BUFFER_SIZE == 0:
            try:
                data += conn.recv(BUFFER_SIZE, MSG_DONTWAIT)
            except socket.error:
                break
        return data
    finally:
        conn.close() 
开发者ID:innogames,项目名称:igcollect,代码行数:16,代码来源:haproxy.py

示例6: test_connect_refused

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_connect_refused(self):
        """
        :py:obj:`Connection.connect` raises :py:obj:`socket.error` if the underlying socket
        connect method raises it.
        """
        client = socket()
        context = Context(TLSv1_METHOD)
        clientSSL = Connection(context, client)
        exc = self.assertRaises(error, clientSSL.connect, ("127.0.0.1", 1))
        self.assertEquals(exc.args[0], ECONNREFUSED) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:12,代码来源:test_ssl.py

示例7: test_connect_ex

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_connect_ex(self):
            """
            If there is a connection error, :py:obj:`Connection.connect_ex` returns the
            errno instead of raising an exception.
            """
            port = socket()
            port.bind(('', 0))
            port.listen(3)

            clientSSL = Connection(Context(TLSv1_METHOD), socket())
            clientSSL.setblocking(False)
            result = clientSSL.connect_ex(port.getsockname())
            expected = (EINPROGRESS, EWOULDBLOCK)
            self.assertTrue(
                    result in expected, "%r not in %r" % (result, expected)) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:17,代码来源:test_ssl.py

示例8: test_shutdown_closed

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_shutdown_closed(self):
        """
        If the underlying socket is closed, :py:obj:`Connection.shutdown` propagates the
        write error from the low level write call.
        """
        server, client = self._loopback()
        server.sock_shutdown(2)
        exc = self.assertRaises(SysCallError, server.shutdown)
        if platform == "win32":
            self.assertEqual(exc.args[0], ESHUTDOWN)
        else:
            self.assertEqual(exc.args[0], EPIPE) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:14,代码来源:test_ssl.py

示例9: test_wantWriteError

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_wantWriteError(self):
        """
        :py:obj:`Connection` methods which generate output raise
        :py:obj:`OpenSSL.SSL.WantWriteError` if writing to the connection's BIO
        fail indicating a should-write state.
        """
        client_socket, server_socket = socket_pair()
        # Fill up the client's send buffer so Connection won't be able to write
        # anything.  Only write a single byte at a time so we can be sure we
        # completely fill the buffer.  Even though the socket API is allowed to
        # signal a short write via its return value it seems this doesn't
        # always happen on all platforms (FreeBSD and OS X particular) for the
        # very last bit of available buffer space.
        msg = b"x"
        for i in range(1024 * 1024 * 4):
            try:
                client_socket.send(msg)
            except error as e:
                if e.errno == EWOULDBLOCK:
                    break
                raise
        else:
            self.fail(
                "Failed to fill socket buffer, cannot test BIO want write")

        ctx = Context(TLSv1_METHOD)
        conn = Connection(ctx, client_socket)
        # Client's speak first, so make it an SSL client
        conn.set_connect_state()
        self.assertRaises(WantWriteError, conn.do_handshake)

    # XXX want_read 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:34,代码来源:test_ssl.py

示例10: test_closed

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_closed(self):
        """
        If the underlying socket is closed, :py:obj:`Connection.sendall` propagates the
        write error from the low level write call.
        """
        server, client = self._loopback()
        server.sock_shutdown(2)
        exc = self.assertRaises(SysCallError, server.sendall, b"hello, world")
        if platform == "win32":
            self.assertEqual(exc.args[0], ESHUTDOWN)
        else:
            self.assertEqual(exc.args[0], EPIPE) 
开发者ID:aliyun,项目名称:oss-ftp,代码行数:14,代码来源:test_ssl.py

示例11: test_syscallError

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_syscallError(self):
        """
        If the underlying C{sendmsg} call fails, L{send1msg} raises
        L{socket.error} with its errno set to the underlying errno value.
        """
        with open(devnull) as probe:
            fd = probe.fileno()
        exc = self.assertRaises(error, send1msg, fd, "hello, world")
        self.assertEqual(exc.args[0], errno.EBADF) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:11,代码来源:test_sendmsg.py

示例12: test_syscallErrorWithControlMessage

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_syscallErrorWithControlMessage(self):
        """
        The behavior when the underlying C{sendmsg} call fails is the same
        whether L{send1msg} is passed ancillary data or not.
        """
        with open(devnull) as probe:
            fd = probe.fileno()
        exc = self.assertRaises(
            error, send1msg, fd, "hello, world", 0, [(0, 0, "0123")])
        self.assertEqual(exc.args[0], errno.EBADF) 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:12,代码来源:test_sendmsg.py

示例13: test_wrongTypeAncillary

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_wrongTypeAncillary(self):
        """
        L{send1msg} will show a helpful exception message when given the wrong
        type of object for the 'ancillary' argument.
        """
        error = self.assertRaises(TypeError,
                                  send1msg, self.input.fileno(),
                                  "hello, world!", 0, 4321)
        self.assertEqual(str(error),
                         "send1msg argument 3 expected list, got int") 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:12,代码来源:test_sendmsg.py

示例14: test_sendmsgTwoAncillaryDoesNotSegfault

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def test_sendmsgTwoAncillaryDoesNotSegfault(self):
        """
        L{sendmsg} with two FDs in two separate ancillary entries
        does not segfault.
        """
        ancillary = [
            (SOL_SOCKET, SCM_RIGHTS, pack("i", self.input.fileno())),
            (SOL_SOCKET, SCM_RIGHTS, pack("i", self.output.fileno())),
        ]
        try:
            send1msg(self.input.fileno(), b"some data", 0, ancillary)
        except error:
            # Ok as long as it doesn't segfault.
            pass 
开发者ID:proxysh,项目名称:Safejumper-for-Desktop,代码行数:16,代码来源:test_sendmsg.py

示例15: socket_any_family

# 需要导入模块: from socket import socket [as 别名]
# 或者: from socket.socket import error [as 别名]
def socket_any_family():
    try:
        return socket(AF_INET)
    except error as e:
        if e.errno == EAFNOSUPPORT:
            return socket(AF_INET6)
        raise 
开发者ID:pyca,项目名称:pyopenssl,代码行数:9,代码来源:test_ssl.py


注:本文中的socket.socket.error方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。