Best Python code snippet using localstack_python
network_unittest.py
Source:network_unittest.py
...51 self.assertTrue(IPy.IP(address) in pubnet)52 self.assertTrue(IPy.IP(address) in self.network.network)53 def test_allocate_deallocate_ip(self):54 address = network.allocate_ip(55 "netuser", "project0", utils.generate_mac())56 logging.debug("Was allocated %s" % (address))57 self.assertEqual(True, address in self._get_project_addresses("project0"))58 rv = network.deallocate_ip(address)59 self.assertEqual(False, address in self._get_project_addresses("project0"))60 def test_range_allocation(self):61 address = network.allocate_ip(62 "netuser", "project0", utils.generate_mac())63 secondaddress = network.allocate_ip(64 "netuser", "project1", utils.generate_mac())65 self.assertEqual(True,66 address in self._get_project_addresses("project0"))67 self.assertEqual(True,68 secondaddress in self._get_project_addresses("project1"))69 self.assertEqual(False, address in self._get_project_addresses("project1"))70 rv = network.deallocate_ip(address)71 self.assertEqual(False, address in self._get_project_addresses("project0"))72 rv = network.deallocate_ip(secondaddress)73 self.assertEqual(False,74 secondaddress in self._get_project_addresses("project1"))75 def test_subnet_edge(self):76 secondaddress = network.allocate_ip("netuser", "project0",77 utils.generate_mac())78 for project in range(1,5):79 project_id = "project%s" % (project)80 address = network.allocate_ip(81 "netuser", project_id, utils.generate_mac())82 address2 = network.allocate_ip(83 "netuser", project_id, utils.generate_mac())84 address3 = network.allocate_ip(85 "netuser", project_id, utils.generate_mac())86 self.assertEqual(False,87 address in self._get_project_addresses("project0"))88 self.assertEqual(False,89 address2 in self._get_project_addresses("project0"))90 self.assertEqual(False,91 address3 in self._get_project_addresses("project0"))92 rv = network.deallocate_ip(address)93 rv = network.deallocate_ip(address2)94 rv = network.deallocate_ip(address3)95 rv = network.deallocate_ip(secondaddress)96 def test_too_many_projects(self):97 for i in range(0, 30):98 name = 'toomany-project%s' % i99 self.manager.create_project(name, 'netuser', name)100 address = network.allocate_ip(101 "netuser", name, utils.generate_mac())102 rv = network.deallocate_ip(address)103 self.manager.delete_project(name)104 def _get_project_addresses(self, project_id):105 project_addresses = []106 for addr in network.get_project_network(project_id).list_addresses():107 project_addresses.append(addr)...
main.py
Source:main.py
...6SALT = get_random_bytes(16)7def generate_key(password):8 key = PBKDF2(password, SALT, 64, 1000, hmac_hash_module=SHA256)9 return key10def generate_mac(text, key):11 h = HMAC.new(key, digestmod=SHA256)12 h.update(text)13 return h.hexdigest()14def encrypt(plaintext, password, mode):15 key = generate_key(password)16 key1 = key[:32]17 key2 = key[32:]18 if mode == 'ETM':19 cipher = AES.new(key1, AES.MODE_CBC)20 ciphertext = cipher.iv + cipher.encrypt(pad(plaintext, AES.block_size))21 signature = generate_mac(ciphertext, key2)22 return ciphertext + signature.encode()23 if mode == 'EAM':24 signature = generate_mac(plaintext, key2)25 cipher = AES.new(key1, AES.MODE_CBC)26 ciphertext = cipher.iv + cipher.encrypt(pad(plaintext, AES.block_size))27 return ciphertext + signature.encode()28 if mode == 'MTE':29 signature = generate_mac(plaintext, key2)30 plaintext = plaintext + signature.encode()31 cipher = AES.new(key1, AES.MODE_CBC)32 ciphertext = cipher.iv + cipher.encrypt(pad(plaintext, AES.block_size))33 return ciphertext34def decrypt(ciphertext, password, mode):35 key = generate_key(password)36 key1 = key[:32]37 key2 = key[32:]38 if mode == "ETM":39 signature = ciphertext[-64:]40 ciphertext = ciphertext[:-64]41 mac = generate_mac(ciphertext, key2)42 iv = ciphertext[:16]43 ciphertext = ciphertext[16:]44 cipher = AES.new(key1, AES.MODE_CBC, iv)45 plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)46 return plaintext.decode(), mac.encode() == signature47 if mode == "EAM":48 signature = ciphertext[-64:]49 ciphertext = ciphertext[:-64]50 iv = ciphertext[:16]51 ciphertext = ciphertext[16:]52 cipher = AES.new(key1, AES.MODE_CBC, iv)53 plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)54 mac = generate_mac(plaintext, key2)55 return plaintext.decode(), mac.encode() == signature56 if mode == "MTE":57 iv = ciphertext[:16]58 ciphertext = ciphertext[16:]59 cipher = AES.new(key1, AES.MODE_CBC, iv)60 plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)61 signature = plaintext[-64:]62 plaintext = plaintext[0:-64]63 mac = generate_mac(plaintext, key2)64 return plaintext.decode(), mac.encode() == signature65if __name__ == '__main__':66 while True:67 text = str(input("Digite um texto ou nome de um arquivo para ser criptografado: "))68 try:69 file = open(text, "rb")70 text = file.read()71 except:72 text = text.encode()73 password = str(input("Digite uma senha: ")).encode()74 mode = input("Digite um modo de encriptação (ETM, EAM ou MTE): ")75 cypertext = encrypt(text, password, mode)76 plaintext, tag = decrypt(cypertext, password, mode)77 ...
random_name.py
Source:random_name.py
1#!/usr/bin/python2#3### 'random_name' Ansible module #############################################4# 2016 - Brian LaShomb <brian.lashomb@target.com>5##############################################################################6DOCUMENTATION = '''7---8module: random_name9short_description: Generates a random MAC Address and/or modifed for use a VM Name.10'''11EXAMPLES = '''12 - name: Generating random name for VM13 random_name: generate_name=True14'''15RETURN = '''16vm_name:17 description: Name of new VM18 returned: success19 type: string20 sample: "edge_00163e1e52d91"21'''22import random23def randomMAC():24 return [ 0x00, 0x16, 0x3e,25 random.randint(0x00, 0x7f),26 random.randint(0x00, 0xff),27 random.randint(0x00, 0xff) ]28def MACprettyprint(mac):29 return ''.join(map(lambda x: "%02x" % x, mac))30def main():31 '''Generates random MAC address and removes the colons'''32 module = AnsibleModule(33 argument_spec = dict(34 generate_name=dict(default=True, choices=[True, False], type='bool'),35 generate_MAC=dict(default=False, choices=[True, False], type='bool')36 )37 )38 generate_name = module.params['generate_name']39 generate_MAC = module.params['generate_MAC']40 if generate_name == True and generate_MAC == True:41 module.exit_json(changed=True, ansible_facts=dict(vm_name="edge_" + MACprettyprint(randomMAC()), mac_addr=(randomMAC())))42 elif generate_name == True:43 module.exit_json(changed=True, ansible_facts=dict(vm_name="edge_" + MACprettyprint(randomMAC())))44 elif generate_MAC == True:45 module.exit_json(changed=True, ansible_facts=dict(mac_addr=(randomMAC())))46# this is magic, see lib/ansible/module_common.py47from ansible.module_utils.basic import *...
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!!