當前位置: 首頁>>代碼示例>>Python>>正文


Python socket.fromfd方法代碼示例

本文整理匯總了Python中socket.fromfd方法的典型用法代碼示例。如果您正苦於以下問題:Python socket.fromfd方法的具體用法?Python socket.fromfd怎麽用?Python socket.fromfd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在socket的用法示例。


在下文中一共展示了socket.fromfd方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _fromListeningDescriptor

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def _fromListeningDescriptor(cls, reactor, fd, addressFamily, factory):
        """
        Create a new L{Port} based on an existing listening I{SOCK_STREAM}
        socket.

        Arguments are the same as to L{Port.__init__}, except where noted.

        @param fd: An integer file descriptor associated with a listening
            socket.  The socket must be in non-blocking mode.  Any additional
            attributes desired, such as I{FD_CLOEXEC}, must also be set already.

        @param addressFamily: The address family (sometimes called I{domain}) of
            the existing socket.  For example, L{socket.AF_INET}.

        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
        """
        port = socket.fromfd(fd, addressFamily, cls.socketType)
        interface = port.getsockname()[0]
        self = cls(None, factory, None, interface, reactor)
        self._preexistingSocket = port
        return self 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:23,代碼來源:tcp.py

示例2: recvfd

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def recvfd(socketfd):
    """
    Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX}
    socket.

    @param socketfd: An C{AF_UNIX} socket, attached to another process waiting
        to send sockets via the ancillary data mechanism in L{send1msg}.

    @param fd: C{int}

    @return: a 2-tuple of (new file descriptor, description).
    @rtype: 2-tuple of (C{int}, C{bytes})
    """
    ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM)
    data, ancillary, flags = recvmsg(ourSocket)
    [(cmsgLevel, cmsgType, packedFD)] = ancillary
    # cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but
    # since those are the *only* standard values, there's not much point in
    # checking.
    [unpackedFD] = unpack("i", packedFD)
    return (unpackedFD, data) 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:23,代碼來源:pullpipe.py

示例3: killSocket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def killSocket(self):
        if not self.currentResponse:
            return

        try:
            socket.fromfd(self.currentResponse.raw.fileno(), socket.AF_INET, socket.SOCK_STREAM).shutdown(socket.SHUT_RDWR)
            return
        except AttributeError:
            pass
        except Exception as e:
            util.ERROR(err=e)

        try:
            self.currentResponse.raw._fp.fp._sock.shutdown(socket.SHUT_RDWR)
        except AttributeError:
            pass
        except Exception as e:
            util.ERROR(err=e) 
開發者ID:plexinc,項目名稱:plex-for-kodi,代碼行數:20,代碼來源:http.py

示例4: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def __init__(self,
                 socketFd,
                 sharedStateAddress):
        self.socketFd = socketFd
        self.callbackSchedulerFactory = CallbackScheduler.createSimpleCallbackSchedulerFactory()

        self.scheduler = self.callbackSchedulerFactory.createScheduler(
            "BackendGatewayRequestHandler",
            1
            )

        sharedStateHost, sharedStatePort = sharedStateAddress.split(':')
        sharedStateViewFactory = ViewFactory.ViewFactory.TcpViewFactory(
            self.callbackSchedulerFactory.createScheduler('SharedState', 1),
            sharedStateHost,
            int(sharedStatePort)
            )

        self.subscribableHandler = ConnectionHandler.ConnectionHandler(
            self.scheduler,
            sharedStateViewFactory,
            lambda: TcpChannelFactory.TcpStringChannelFactory(self.scheduler)
            )
        self.sock = socket.fromfd(socketFd, socket.AF_INET, socket.SOCK_STREAM) 
開發者ID:ufora,項目名稱:ufora,代碼行數:26,代碼來源:handleBackendGatewayConnection.py

示例5: get_high_socket_fd

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def get_high_socket_fd(self):
        if WIN32:
            # The child process will not have any socket handles, so
            # calling socket.fromfd() should produce WSAENOTSOCK even
            # if there is a handle of the same number.
            return socket.socket().detach()
        else:
            # We want to produce a socket with an fd high enough that a
            # freshly created child process will not have any fds as high.
            fd = socket.socket().detach()
            to_close = []
            while fd < 50:
                to_close.append(fd)
                fd = os.dup(fd)
            for x in to_close:
                os.close(x)
            return fd 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:19,代碼來源:_test_multiprocessing.py

示例6: _fromListeningDescriptor

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def _fromListeningDescriptor(cls, reactor, fd, addressFamily, factory):
        """
        Create a new L{Port} based on an existing listening I{SOCK_STREAM}
        socket.

        Arguments are the same as to L{Port.__init__}, except where noted.

        @param fd: An integer file descriptor associated with a listening
            socket.  The socket must be in non-blocking mode.  Any additional
            attributes desired, such as I{FD_CLOEXEC}, must also be set already.

        @param addressFamily: The address family (sometimes called I{domain}) of
            the existing socket.  For example, L{socket.AF_INET}.

        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
        """
        port = socket.fromfd(fd, addressFamily, cls.socketType)
        interface = _getsockname(port)[0]
        self = cls(None, factory, None, interface, reactor)
        self._preexistingSocket = port
        return self 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:23,代碼來源:tcp.py

示例7: _fromListeningDescriptor

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def _fromListeningDescriptor(cls, reactor, fd, factory):
        """
        Create a new L{Port} based on an existing listening I{SOCK_STREAM}
        socket.

        Arguments are the same as to L{Port.__init__}, except where noted.

        @param fd: An integer file descriptor associated with a listening
            socket.  The socket must be in non-blocking mode.  Any additional
            attributes desired, such as I{FD_CLOEXEC}, must also be set already.

        @return: A new instance of C{cls} wrapping the socket given by C{fd}.
        """
        port = socket.fromfd(fd, cls.addressFamily, cls.socketType)
        self = cls(port.getsockname(), factory, reactor=reactor)
        self._preexistingSocket = port
        return self 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:19,代碼來源:unix.py

示例8: is_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def is_socket(fd):
    """ Determine if the file descriptor is a socket.

        Return ``False`` if querying the socket type of `fd` raises an
        error; otherwise return ``True``.

        """
    result = False

    file_socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)

    try:
        socket_type = file_socket.getsockopt(
            socket.SOL_SOCKET, socket.SO_TYPE)
    except socket.error, exc:
        exc_errno = exc.args[0]
        if exc_errno == errno.ENOTSOCK:
            # Socket operation on non-socket
            pass
        else:
            # Some other socket error
            result = True 
開發者ID:blackye,項目名稱:luscan-devel,代碼行數:24,代碼來源:daemon.py

示例9: _is_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def _is_socket(cls, stream):
        """Check if the given stream is a socket."""
        try:
            fd = stream.fileno()
        except ValueError:
            # If it has no file descriptor, it's not a socket
            return False

        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
        try:
            # This will raise a socket.error if it's not a socket
            sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
        except socket.error as ex:
            if ex.args[0] != errno.ENOTSOCK:
                # It must be a socket
                return True
        else:
            # If an exception wasn't raised, it's a socket
            return True 
開發者ID:jnrbsn,項目名稱:daemonocle,代碼行數:21,代碼來源:core.py

示例10: _get_systemd_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def _get_systemd_socket(self, address):
        fds = sd.listen_fds()
        if not fds:
            return address
        elif len(fds) > 1:
            raise ValueError('Too many listening sockets', fds)

        if isinstance(address, tuple):
            port = address[1]
            # systemd uses IPv6
            if not sd.is_socket_inet(fds[0], family=socket.AF_INET6,
                                     type=socket.SOCK_STREAM,
                                     listening=True, port=port):
                raise ValueError(
                    "FD {} is not TCP IPv6 socket on port {}".format(
                        fds[0], port
                    )
                )
            logger.info('Using systemd socket activation on port %i', port)
            sock = socket.fromfd(fds[0], socket.AF_INET6, socket.SOCK_STREAM)
        else:
            if not sd.is_socket_unix(fds[0], socket.SOCK_STREAM,
                                     listening=True, path=address):
                raise ValueError(
                    "FD {} is not Unix stream socket on path {}".format(
                        fds[0], address
                    )
                )
            logger.info('Using systemd socket activation on path %s', address)
            sock = socket.fromfd(fds[0], socket.AF_UNIX, socket.SOCK_STREAM)

        if sys.version_info[0] < 3:
            # Python 2.7's socket.fromfd() returns _socket.socket
            sock = socket.socket(_sock=sock)
        return sock 
開發者ID:latchset,項目名稱:custodia,代碼行數:37,代碼來源:server.py

示例11: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def __init__(self, host, port, app, handler=None,
                 passthrough_errors=False, ssl_context=None, fd=None):
        if handler is None:
            handler = WSGIRequestHandler

        self.address_family = select_ip_version(host, port)

        if fd is not None:
            real_sock = socket.fromfd(fd, self.address_family,
                                      socket.SOCK_STREAM)
            port = 0
        HTTPServer.__init__(self, (host, int(port)), handler)
        self.app = app
        self.passthrough_errors = passthrough_errors
        self.shutdown_signal = False
        self.host = host
        self.port = port

        # Patch in the original socket.
        if fd is not None:
            self.socket.close()
            self.socket = real_sock
            self.server_address = self.socket.getsockname()

        if ssl_context is not None:
            if isinstance(ssl_context, tuple):
                ssl_context = load_ssl_context(*ssl_context)
            if ssl_context == 'adhoc':
                ssl_context = generate_adhoc_ssl_context()
            # If we are on Python 2 the return value from socket.fromfd
            # is an internal socket object but what we need for ssl wrap
            # is the wrapper around it :(
            sock = self.socket
            if PY2 and not isinstance(sock, socket.socket):
                sock = socket.socket(sock.family, sock.type, sock.proto, sock)
            self.socket = ssl_context.wrap_socket(sock, server_side=True)
            self.ssl_context = ssl_context
        else:
            self.ssl_context = None 
開發者ID:jpush,項目名稱:jbox,代碼行數:41,代碼來源:serving.py

示例12: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def __init__(self, address, conf, log, fd=None):
        self.log = log
        self.conf = conf

        self.cfg_addr = address
        if fd is None:
            sock = socket.socket(self.FAMILY, socket.SOCK_STREAM)
        else:
            sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM)

        self.sock = self.set_options(sock, bound=(fd is not None)) 
開發者ID:jpush,項目名稱:jbox,代碼行數:13,代碼來源:sock.py

示例13: is_socket

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def is_socket (fd):
		try:
			s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
		except ValueError,e:
			# The file descriptor is closed
			return False 
開發者ID:Exa-Networks,項目名稱:exaddos,代碼行數:8,代碼來源:application.py

示例14: __init__

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def __init__(self, host, port, app, handler=None,
                 passthrough_errors=False, ssl_context=None, fd=None):
        if handler is None:
            handler = WSGIRequestHandler

        self.address_family = select_ip_version(host, port)

        if fd is not None:
            real_sock = socket.fromfd(fd, self.address_family,
                                      socket.SOCK_STREAM)
            port = 0
        HTTPServer.__init__(self, get_sockaddr(host, int(port),
                                               self.address_family), handler)
        self.app = app
        self.passthrough_errors = passthrough_errors
        self.shutdown_signal = False
        self.host = host
        self.port = self.socket.getsockname()[1]

        # Patch in the original socket.
        if fd is not None:
            self.socket.close()
            self.socket = real_sock
            self.server_address = self.socket.getsockname()

        if ssl_context is not None:
            if isinstance(ssl_context, tuple):
                ssl_context = load_ssl_context(*ssl_context)
            if ssl_context == 'adhoc':
                ssl_context = generate_adhoc_ssl_context()
            # If we are on Python 2 the return value from socket.fromfd
            # is an internal socket object but what we need for ssl wrap
            # is the wrapper around it :(
            sock = self.socket
            if PY2 and not isinstance(sock, socket.socket):
                sock = socket.socket(sock.family, sock.type, sock.proto, sock)
            self.socket = ssl_context.wrap_socket(sock, server_side=True)
            self.ssl_context = ssl_context
        else:
            self.ssl_context = None 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:42,代碼來源:serving.py

示例15: testFromFd

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import fromfd [as 別名]
def testFromFd(self):
        # Testing fromfd()
        fd = self.cli_conn.fileno()
        sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
        self.addCleanup(sock.close)
        msg = sock.recv(1024)
        self.assertEqual(msg, MSG) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:9,代碼來源:test_socket.py


注:本文中的socket.fromfd方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。