本文整理匯總了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