Best Python code snippet using autotest_python
ip.py
Source:ip.py
12def ip_to_long(ip):3 """Converts a dotted-quad IP to a long.4 5 Input: "192.168.0.1"6 Output: 3232235521L7 """8 import struct, socket9 return struct.unpack(">L", socket.inet_aton(ip))[0]1011def long_to_ip(long):12 """Converts a long IP to a dotted-quad string.13 14 Input: 3232235521L15 Output: "192.168.0.1"16 """ 17 import struct, socket18 return socket.inet_ntoa(struct.pack(">L", long))1920212223#Helper Functions relying on external data:24External_IP = None2526class IPError:27 def __init__(self, error=""):28 self.error = error29 def __repr__(self):30 return self.error3132def ip_to_long_external(ip):33 """Converts a dotted-quad IP to a long, while automaticly changing it to an external IP.3435 Relies on a global named External_IP, which is a string in dotted-quad IP notation3637 Assuming your WAN IP is 69.123.251.160,38 Input: "192.168.0.1"39 Output: 1165753248L (69.123.251.160 encoded)40 """41 global External_IP42 dip = ip_to_long(ip) #Assume ip is okay43 foct = long(ip.split('.')[0]) #grab first octet44 internalipoct = (192, 127)45 if(foct in internalipoct): #check it against pre-defined "internal" list46 if External_IP == None:47 raise IPError48 dip = ip_to_long(External_IP)49 return dip5051def get_external_ip(nocache=False):52 """Gets external IP from checkdns.dyndns.org, and sets global External_IP53 Warning: no timeout"""54 global External_IP55 if (External_IP != None) and (nocache != True):56 return External_IP57 try:58 import urllib59 u = urllib.urlopen("http://checkip.dyndns.org/")60 n = u.read(1024)61 u.close()62 import re63 ip = re.findall("<body>Current IP Address: ([^<]*)</body>", n)64 if len(ip) == 0:65 raise IPError("Unable to get IP from http://checkip.dyndns.org/: %s"%(n))66 ip = ip[0]67 External_IP = ip68 return ip69 except:70 raise IPError("Unable to get IP from http://checkip.dyndns.org/")71 return None727374if __name__ == "__main__":75 import unittest7677 class IPConversionTestCase(unittest.TestCase):78 def runTest(self):79 assert ip_to_long("192.168.0.1") == 3232235521L, 'ip_to_long failure'80 assert long_to_ip(3232235521L) == "192.168.0.1", 'long_to_ip failure'81 #print "IP converstion tests OK"82 class ExternalIPTestcase(unittest.TestCase):83 def runTest(self):84 ip = get_external_ip()85 #print "External IP is %s"%(ip)86 assert ip != None, 'get_external_ip() failed'87 import time88 print time.strftime("[%Y-%m-%d %H:%M]: ") + "Running ip.py testcases..."
...
models.py
Source:models.py
...3from ipauth import models4class IPToLongTestCase(TestCase):5 def test_valid_ip(self):6 """ Test the ip_to_long function with valid IP addresses """7 self.assertEqual(models.ip_to_long('128.1.1.1'), 2147549441)8 self.assertEqual(models.ip_to_long('128.1.0.1'), 2147549185)9 self.assertEqual(models.ip_to_long('128.0.1.1'), 2147483905)10 self.assertEqual(models.ip_to_long('128.1.1.0'), 2147549440)11 self.assertEqual(models.ip_to_long('10.0.0.0'), 167772160)12 13 def test_invalid_ip(self):14 """ Test the ip_to_long function with some invalid IP addresses. """15 self.assertRaises(ValidationError, models.ip_to_long,16 'completely invalid')17 self.assertRaises(ValidationError, models.ip_to_long,18 '128.0.0.a')19 self.assertRaises(ValidationError, models.ip_to_long,20 '256.0.0.1')21class LongToIPTestCase(TestCase):22 def test_valid_ip(self):23 """ Test the long_to_ip function with valid IP addresses """24 self.assertEqual(models.long_to_ip(2147549441), '128.1.1.1')25 self.assertEqual(models.long_to_ip(2147549185), '128.1.0.1')...
Check out the latest blogs from LambdaTest on this topic:
The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.
One of the most important skills for leaders to have is the ability to prioritize. To understand how we can organize all of the tasks that must be completed in order to complete a project, we must first understand the business we are in, particularly the project goals. There might be several project drivers that stimulate project execution and motivate a company to allocate the appropriate funding.
Web applications continue to evolve at an unbelievable pace, and the architecture surrounding web apps get more complicated all of the time. With the growth in complexity of the web application and the development process, web application testing also needs to keep pace with the ever-changing demands.
In some sense, testing can be more difficult than coding, as validating the efficiency of the test cases (i.e., the ‘goodness’ of your tests) can be much harder than validating code correctness. In practice, the tests are just executed without any validation beyond the pass/fail verdict. On the contrary, the code is (hopefully) always validated by testing. By designing and executing the test cases the result is that some tests have passed, and some others have failed. Testers do not know much about how many bugs remain in the code, nor about their bug-revealing efficiency.
Howdy testers! If you’re reading this article I suggest you keep a diary & a pen handy because we’ve added numerous exciting features to our cross browser testing cloud and I am about to share them with you right away!
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!!