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


Python socket.getsockname方法代碼示例

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


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

示例1: findFreePort

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getsockname [as 別名]
def findFreePort(interface='127.0.0.1', family=socket.AF_INET,
                 type=socket.SOCK_STREAM):
    """
    Ask the platform to allocate a free port on the specified interface, then
    release the socket and return the address which was allocated.

    @param interface: The local address to try to bind the port on.
    @type interface: C{str}

    @param type: The socket type which will use the resulting port.

    @return: A two-tuple of address and port, like that returned by
        L{socket.getsockname}.
    """
    addr = socket.getaddrinfo(interface, 0)[0][4]
    probe = socket.socket(family, type)
    try:
        probe.bind(addr)
        return probe.getsockname()
    finally:
        probe.close() 
開發者ID:proxysh,項目名稱:Safejumper-for-Desktop,代碼行數:23,代碼來源:connectionmixins.py

示例2: find_free_port

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getsockname [as 別名]
def find_free_port(interface='127.0.0.1', socket_family=socket.AF_INET,
                   socket_type=socket.SOCK_STREAM):
    """
    Ask the platform to allocate a free port on the specified interface, then
    release the socket and return the address which was allocated.

    Copied from ``twisted.internet.test.connectionmixins.findFreePort``.

    :param bytes interface: The local address to try to bind the port on.
    :param int socket_family: The socket family of port.
    :param int socket_type: The socket type of the port.

    :return: A two-tuple of address and port, like that returned by
        ``socket.getsockname``.
    """
    address = socket.getaddrinfo(interface, 0)[0][4]
    probe = socket.socket(socket_family, socket_type)
    try:
        probe.bind(address)
        return probe.getsockname()
    finally:
        probe.close() 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:24,代碼來源:__init__.py

示例3: findFreePort

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getsockname [as 別名]
def findFreePort(interface='127.0.0.1', family=socket.AF_INET,
                 type=socket.SOCK_STREAM):
    """
    Ask the platform to allocate a free port on the specified interface, then
    release the socket and return the address which was allocated.

    @param interface: The local address to try to bind the port on.
    @type interface: C{str}

    @param type: The socket type which will use the resulting port.

    @return: A two-tuple of address and port, like that returned by
        L{socket.getsockname}.
    """
    addr = socket.getaddrinfo(interface, 0)[0][4]
    probe = socket.socket(family, type)
    try:
        probe.bind(addr)
        if family == socket.AF_INET6:
            sockname = probe.getsockname()
            hostname = socket.getnameinfo(
                sockname, socket.NI_NUMERICHOST | socket.NI_NUMERICSERV)[0]
            return (hostname, sockname[1])
        else:
            return probe.getsockname()
    finally:
        probe.close() 
開發者ID:wistbean,項目名稱:learn_python3_spider,代碼行數:29,代碼來源:connectionmixins.py

示例4: get_port

# 需要導入模塊: import socket [as 別名]
# 或者: from socket import getsockname [as 別名]
def get_port(socket):
    """Return the port to which a socket is bound."""
    addr, port = socket.getsockname()
    return port 
開發者ID:maas,項目名稱:maas,代碼行數:6,代碼來源:bindfixture.py


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