Python socket.getsockname() Examples
The following are 4
code examples of socket.getsockname().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: connectionmixins.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
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()
Example #2
Source File: __init__.py From flocker with Apache License 2.0 | 6 votes |
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()
Example #3
Source File: connectionmixins.py From learn_python3_spider with MIT License | 5 votes |
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()
Example #4
Source File: bindfixture.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def get_port(socket): """Return the port to which a socket is bound.""" addr, port = socket.getsockname() return port