本文整理汇总了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()
示例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()
示例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()
示例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