How to use is_port_free method in avocado

Best Python code snippet using avocado_python

test_utils_network.py

Source: test_utils_network.py Github

copy

Full Screen

...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:...

Full Screen

Full Screen

network.py

Source: network.py Github

copy

Full Screen

...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...

Full Screen

Full Screen

ports.py

Source: ports.py Github

copy

Full Screen

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:...

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Nov’22 Updates: Live With Automation Testing On OTT Streaming Devices, Test On Samsung Galaxy Z Fold4, Galaxy Z Flip4, &#038; More

Hola Testers! Hope you all had a great Thanksgiving weekend! To make this time more memorable, we at LambdaTest have something to offer you as a token of appreciation.

Considering Agile Principles from a different angle

In addition to the four values, the Agile Manifesto contains twelve principles that are used as guides for all methodologies included under the Agile movement, such as XP, Scrum, and Kanban.

How To Choose The Best JavaScript Unit Testing Frameworks

JavaScript is one of the most widely used programming languages. This popularity invites a lot of JavaScript development and testing frameworks to ease the process of working with it. As a result, numerous JavaScript testing frameworks can be used to perform unit testing.

How To Write End-To-End Tests Using Cypress App Actions

When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.

[LambdaTest Spartans Panel Discussion]: What Changed For Testing &#038; QA Community And What Lies Ahead

The rapid shift in the use of technology has impacted testing and quality assurance significantly, especially around the cloud adoption of agile development methodologies. With this, the increasing importance of quality and automation testing has risen enough to deliver quality work.

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful