Best Python code snippet using ATX
test_browser.py
Source:test_browser.py
...82 assert hubcheck.conf.settings.proxy is None83 proxy = Proxy()84 proxy.start()85 assert hubcheck.conf.settings.proxy is not None86 assert is_port_listening('localhost',proxy.port) is True87 assert os.path.isfile('/tmp/userAgentString.properties')88 assert os.path.isfile('/tmp/userAgentString.txt')89 def test_stop_1(self):90 """91 stop the proxy server92 at the end of the process the following should be true:93 1. hubcheck.conf.settings.proxy is None94 2. proxy.port is not listening95 """96 proxy = Proxy()97 proxy.start()98 proxy.stop()99 assert hubcheck.conf.settings.proxy is None100 assert is_port_listening('localhost',proxy.port) is False101 # stop() doesn't clean up tmp files.102 # assert not os.path.isfile('/tmp/userAgentString.properties')103 # assert not os.path.isfile('/tmp/userAgentString.txt')104 def test_create_client_1(self,proxy):105 """106 create a proxy client on the default port107 """108 # make sure the potential client port is not occupied109 assert not is_port_listening("localhost",proxy.port+1)110 # start the client111 proxy_client = proxy.create_client()112 assert is_port_listening("localhost",proxy.port+1)113 def test_create_client_2(self,proxy):114 """115 create a proxy client on the default port116 """117 # make sure the potential client port is not occupied118 assert not is_port_listening("localhost",proxy.port+1)119 # start the client120 proxy_client = proxy.create_client(proxy.port+1)...
temporary_daemon.py
Source:temporary_daemon.py
...17import logging18import subprocess19import tempfile20import time21def is_port_listening(port):22 import socket23 s = socket.socket()24 try:25 s.connect(('localhost', port))26 except socket.error:27 return False28 s.close()29 return True30TEST_PORT=1234531class TemporaryDaemon(object):32 def __init__(self):33 # If a daemon is running, try killing it via /kill34 if is_port_listening(TEST_PORT):35 for i in range(2):36 logging.warn("Existing daemon found. Asking it to exit")37 try:38 conn = httplib.HTTPConnection('localhost', TEST_PORT, True)39 conn.request('GET', '/exit')40 except:41 break42 res = conn.getresponse()43 if res.status != 200:44 break45 else:46 time.sleep(0.2)47 if is_port_listening(TEST_PORT):48 raise Exception("Daemon running")49 self.daemon_settings_file = tempfile.NamedTemporaryFile()50 args = ['./quickopend', 'run', '--settings', self.daemon_settings_file.name, '--port', str(TEST_PORT)]51 args.append('--test')52 self.proc = subprocess.Popen(args)53 ok = False54 for i in range(10):55 try:56 conn = httplib.HTTPConnection('localhost', TEST_PORT, True)57 conn.request('GET', '/ping')58 except:59 time.sleep(0.1)60 continue61 res = conn.getresponse()...
proc.py
Source:proc.py
1import subprocess2import socket3import psutil4def is_port_listening(port, type_="tcp"):5 """check if the port is being used by any process6 Args:7 port (int): port number8 Returns:9 Bool: True if port is used else False10 """11 result = None12 if type_ == "tcp":13 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:14 result = s.connect_ex(("127.0.0.1", port))15 else:16 with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:17 result = s.connect_ex(("127.0.0.1", port))18 # print(f"checking port {port} result was {result}")19 return result == 020class Process:21 def __init__(self, cmd, use_shell=False, env=None, run_once=False, cmd_check="", tcp_ports=None, udp_ports=None, deps=None, log_sinks=None ):22 self.cmd = cmd23 self.env = env or {}24 self.cmd_check = cmd_check25 self.run_once = run_once26 self.tcp_ports = tcp_ports or []27 self.udp_ports = udp_ports or []28 self.deps = deps or [] 29 self.use_shell = use_shell30 self.proc_object = None31 self.log_sinks = log_sinks or []32 def __str__(self):33 return f"Proc {self.cmd}, {self.cmd_check}, {self.tcp_ports}, {self.udp_ports}, {self.deps}"34 def is_running(self):35 # DON'T RESTORE PSUTIL36 # print("checking pid: ", self.proc_object.pid)37 # if not psutil.pid_exists(self.proc_object.pid):38 # return False39 # print(self)40 # print(f"checking command is running")41 if self.cmd_check:42 # print(f"checking by command {self.cmd_check}")43 try:44 res = subprocess.check_call(self.cmd_check, shell=True, stdout=subprocess.DEVNULL)45 except subprocess.CalledProcessError:46 # print("not runnning..., cmd_check")47 return False48 else:49 # print("running...")50 return True51 if self.tcp_ports:52 for p in self.tcp_ports:53 # print("checking against tcp port ",p )54 if not is_port_listening(p):55 # print("port reason")56 return False57 if self.udp_ports:58 for p in self.udp_ports:59 # print("checking against udp port ", p)60 if not is_port_listening(p, "udp"):61 # print("port reason")62 return False63 # print(f"{self.cmd} is running.... correctly after checks")64 return True65 def run(self):66 p = subprocess.Popen(self.cmd, shell=self.use_shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=self.env)67 self.proc_object = p...
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!!