Best Python code snippet using localstack_python
test_helpers.py
Source:test_helpers.py
...192 MockFuture.return_value = expected193 future = helpers.create_future(mock_loop)194 MockFuture.assert_called_with(loop=mock_loop)195 assert expected == future196def test_is_ip_address():197 assert helpers.is_ip_address("127.0.0.1")198 assert helpers.is_ip_address("::1")199 assert helpers.is_ip_address("FE80:0000:0000:0000:0202:B3FF:FE1E:8329")200 # Hostnames201 assert not helpers.is_ip_address("localhost")202 assert not helpers.is_ip_address("www.example.com")203 # Out of range204 assert not helpers.is_ip_address("999.999.999.999")205 # Contain a port206 assert not helpers.is_ip_address("127.0.0.1:80")207 assert not helpers.is_ip_address("[2001:db8:0:1]:80")208 # Too many "::"209 assert not helpers.is_ip_address("1200::AB00:1234::2552:7777:1313")210def test_is_ip_address_bytes():211 assert helpers.is_ip_address(b"127.0.0.1")212 assert helpers.is_ip_address(b"::1")213 assert helpers.is_ip_address(b"FE80:0000:0000:0000:0202:B3FF:FE1E:8329")214 # Hostnames215 assert not helpers.is_ip_address(b"localhost")216 assert not helpers.is_ip_address(b"www.example.com")217 # Out of range218 assert not helpers.is_ip_address(b"999.999.999.999")219 # Contain a port220 assert not helpers.is_ip_address(b"127.0.0.1:80")221 assert not helpers.is_ip_address(b"[2001:db8:0:1]:80")222 # Too many "::"223 assert not helpers.is_ip_address(b"1200::AB00:1234::2552:7777:1313")224def test_ip_addresses():225 ip_addresses = [226 '0.0.0.0',227 '127.0.0.1',228 '255.255.255.255',229 '0:0:0:0:0:0:0:0',230 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',231 '00AB:0002:3008:8CFD:00AB:0002:3008:8CFD',232 '00ab:0002:3008:8cfd:00ab:0002:3008:8cfd',233 'AB:02:3008:8CFD:AB:02:3008:8CFD',234 'AB:02:3008:8CFD::02:3008:8CFD',235 '::',236 '1::1',237 ]238 for address in ip_addresses:239 assert helpers.is_ip_address(address)240def test_host_addresses():241 hosts = [242 'www.four.part.host'243 'www.python.org',244 'foo.bar',245 'localhost',246 ]247 for host in hosts:248 assert not helpers.is_ip_address(host)249def test_is_ip_address_invalid_type():250 with pytest.raises(TypeError):251 helpers.is_ip_address(123)252 with pytest.raises(TypeError):...
ip_task.py
Source:ip_task.py
1import unittest2def is_ip_address(addr):3 if '.' in addr and ':' not in addr:4 return is_ipv4(addr)5 elif '.' not in addr and ':' in addr:6 return is_ipv6(addr)7 else:8 return False9def is_ipv4(v4_address):10 """11 å¤ææ¯å¦ä¸ºipv4å°å12 :param v4_address:13 :return:14 """15 ip_segments = v4_address.split('.')16 # ipv4çå°å为å段17 if len(ip_segments) != 4:18 return False19 for segment in ip_segments:20 # æ¯æ®µæé¿ä¸è½è¶
è¿4个ï¼æ大为25521 if len(segment) > 3:22 return False23 # æ¯æ®µå¿
须为æ°å24 if not segment.isdigit():25 return False26 num = int(segment)27 # æ¯æ®µå¿
é¡»å¨0-255ä¹é´28 if num < 0 or num > 255:29 return False30 return True31def is_ipv6(v6_address):32 """33 å¤ææ¯å¦ä¸ºipv6å°å34 :param v6_address:35 :return:36 """37 ip_segments = v6_address.split(':')38 # ipv6æå¤8段ï¼ä»¥åå·åå²39 if len(ip_segments) > 8:40 return False41 omit_count = 042 for segment in ip_segments:43 if segment == '':44 omit_count += 145 # çç¥0ä¸è½è¶
è¿ä¸¤æ¬¡46 if omit_count > 1:47 return False48 continue49 if segment == '0':50 continue51 # é¤0å¤å¿
é¡»æ¯å°äº4个16è¿å¶å符ç»æ52 if len(segment) > 4:53 return False54 # 16è¿å¶ä»¥æ°å0-9ååæ¯a-f表示55 if not segment.isalnum():56 return False57 # 16è¿å¶åæ¯å¿
é¡»å¨a-fä¹é´58 if segment.isalpha():59 for al in segment:60 if al not in ['A', 'B', 'C', 'D', 'E', 'F', 'a', 'b', 'c', 'd', 'e', 'f']:61 return False62 return True63class TestIsIPAddress(unittest.TestCase):64 def test_ip_address(self):65 self.assertFalse(is_ip_address("aaaaaaaaaabbbbbbbb"))66 self.assertFalse(is_ip_address("127.0.0.1.2"))67 self.assertFalse(is_ip_address("1234.0.0.1"))68 self.assertFalse(is_ip_address("a.b.0.c"))69 self.assertFalse(is_ip_address("256.0.0.1"))70 self.assertFalse(is_ip_address("127.0.0.-1"))71 self.assertTrue(is_ip_address("127.0.0.1"))72 self.assertFalse(is_ip_address("FF60::2A90:FA:0:4CA2:9C5A:0:A"))73 self.assertFalse(is_ip_address("FF60::2A90:FA:0:4CA2:9C5A::"))74 self.assertFalse(is_ip_address("FF60::2A90:ABCDDD:0:4CA2:9C5A:0"))75 self.assertFalse(is_ip_address("FF60::2A90:FA*(:0:4CA2:9C5A:0"))76 self.assertFalse(is_ip_address("FF60::2A90:FH:0:4CA2:9C5A:0"))77 self.assertTrue(is_ip_address("FF60::2A90:FA:0:4CA2:9C5A:0"))78if __name__ == '__main__':...
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!!