當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。