当前位置: 首页>>代码示例>>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;未经允许,请勿转载。