Best Python code snippet using avocado_python
test_utils_network.py
Source:test_utils_network.py
...41 return ipv4_addrs, ipv6_addrs42class FreePort(unittest.TestCase):43 @unittest.skipUnless(HAS_NETIFACES,44 "netifaces library not available")45 def test_is_port_free(self):46 port = ports.find_free_port(sequent=False)47 self.assertTrue(ports.is_port_free(port, "localhost"))48 local_addrs = get_all_local_addrs()49 ipv4_addrs = ["localhost", ""] + list(local_addrs[0])50 ipv6_addrs = ["localhost", ""] + list(local_addrs[1])51 good = []52 bad = []53 skip = []54 sock = None55 for family in ports.FAMILIES:56 if family == socket.AF_INET:57 addrs = ipv4_addrs58 else:59 addrs = ipv6_addrs60 for addr in addrs:61 for protocol in ports.PROTOCOLS:62 try:63 sock = socket.socket(family, protocol)64 sock.bind((addr, port))65 if ports.is_port_free(port, "localhost"):66 bad.append("%s, %s, %s: reports free"67 % (family, protocol, addr))68 else:69 good.append("%s, %s, %s" % (family, protocol,70 addr))71 except Exception as exc:72 if getattr(exc, 'errno', None) in (-2, 2, 22, 94):73 skip.append("%s, %s, %s: Not supported: %s"74 % (family, protocol, addr, exc))75 else:76 bad.append("%s, %s, %s: Failed to bind: %s"77 % (family, protocol, addr, exc))78 finally:79 if sock is not None:...
network.py
Source:network.py
...14"""15Module with network related utility functions16"""17import socket18def is_port_free(port, address):19 """20 Return True if the given port is available for use.21 :param port: Port number22 """23 try:24 s = socket.socket()25 if address == "localhost":26 s.bind((address, port))27 free = True28 else:29 s.connect((address, port))30 free = False31 except socket.error:32 if address == "localhost":33 free = False34 else:35 free = True36 s.close()37 return free38def find_free_port(start_port, end_port, address="localhost"):39 """40 Return a host free port in the range [start_port, end_port].41 :param start_port: First port that will be checked.42 :param end_port: Port immediately after the last one that will be checked.43 """44 for i in range(start_port, end_port):45 if is_port_free(i, address):46 return i47 return None48def find_free_ports(start_port, end_port, count, address="localhost"):49 """50 Return count of host free ports in the range [start_port, end_port].51 :param count: Initial number of ports known to be free in the range.52 :param start_port: First port that will be checked.53 :param end_port: Port immediately after the last one that will be checked.54 """55 ports = []56 i = start_port57 while i < end_port and count > 0:58 if is_port_free(i, address):59 ports.append(i)60 count -= 161 i += 1...
ports.py
Source:ports.py
1""" Utilities for ports. """2import socket3import errno4def is_port_free(port):5 """ Check whether the port is free to use on localhost.6 Parameters7 ----------8 port: int9 Port number to check.10 Returns11 -------12 bool:13 True if free, false if already in use.14 """15 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)16 try:17 s.bind(("127.0.0.1", port))18 rv = True19 except socket.error as e:20 if e.errno == errno.EADDRINUSE:21 rv = False22 else:23 # something else raised the socket.error exception24 raise25 s.close()26 return rv27def get_next_free_port(port):28 """ Find the next free port on localhost.29 30 Recursively test all port numbers with increments of 1 31 starting from number 'port'.32 33 Parameters34 ----------35 port: int36 Start looking from this port number upwards.37 38 Returns39 -------40 int41 Next free port number.42 """43 if is_port_free(port):44 return port45 else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!