Python socket.htonl() Examples
The following are 30
code examples of socket.htonl().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: WXBizMsgCrypt.py From wechat-encrypt-python3 with Apache License 2.0 | 6 votes |
def encrypt(self,text,appid): """对明文进行加密 @param text: 需要加密的明文 @return: 加密得到的字符串 """ # 16位随机字符串添加到明文开头 text_bytes = to_utf8_bytes(text) text_bytes = to_utf8_bytes(self.get_random_str()) + struct.pack("I", socket.htonl( len(text_bytes))) + text_bytes + to_utf8_bytes(appid) # 使用自定义的填充方式对明文进行补位填充 pkcs7 = PKCS7Encoder() text_bytes = pkcs7.encode(text_bytes) # 加密 cryptor = AES.new(self.key,self.mode,self.key[:16]) try: ciphertext = cryptor.encrypt(text_bytes) # 使用BASE64对加密后的字符串进行编码 return ierror.WXBizMsgCrypt_OK, utf8_bytes_to_str(base64.b64encode(ciphertext)) except Exception as e: logger.exception('wechat encryption/decryption error') return ierror.WXBizMsgCrypt_EncryptAES_Error,None
Example #2
Source File: WXBizMsgCrypt.py From wechat_mall with MIT License | 6 votes |
def encrypt(self, text, appid): """对明文进行加密 @param text: 需要加密的明文 @return: 加密得到的字符串 """ # 16位随机字符串添加到明文开头 pack_str = struct.pack(b"I", socket.htonl(len(text))) text = smart_bytes(self.get_random_str()) + pack_str + smart_bytes(text) + smart_bytes(appid) # 使用自定义的填充方式对明文进行补位填充 pkcs7 = PKCS7Encoder() text = pkcs7.encode(text) # 加密 cryptor = AES.new(self.key, self.mode, self.key[:16]) try: ciphertext = cryptor.encrypt(text) # 使用BASE64对加密后的字符串进行编码 return WXBizMsgCrypt_OK, base64.b64encode(ciphertext) except Exception: return WXBizMsgCrypt_EncryptAES_Error, None
Example #3
Source File: pkt-trace.py From TupleNet with Apache License 2.0 | 6 votes |
def parse_trace_path(trace_path): properties = trace_path.split(',') for p in properties: pname, pval = p.split('=') if pname == 'table_id': table_id = pval continue if pname == 'datapath_id': datapath_id = pval continue if pname == 'src_port_id': src_port_id = pval continue if pname == 'dst_port_id': dst_port_id = pval continue if pname == 'tun_src': ip_int = int(pval) tun_src = socket.inet_ntoa(struct.pack('I',socket.htonl(ip_int))) continue if pname == 'output_iface_id': iface_id = pval return table_id, datapath_id, src_port_id, dst_port_id, tun_src, iface_id
Example #4
Source File: 2_3_chat_server_with_select.py From Python-Network-Programming-Cookbook-Second-Edition with MIT License | 5 votes |
def send(channel, *args): buffer = pickle.dumps(args) value = socket.htonl(len(buffer)) size = struct.pack("L",value) channel.send(size) channel.send(buffer)
Example #5
Source File: test_socket.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #6
Source File: test_socket.py From gcblue with BSD 3-Clause "New" or "Revised" License | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1L, 2L, 3L ] bad_values = [ -1, -2, -3, -1L, -2L, -3L ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #7
Source File: hwfw.py From hwfw-tool with GNU Affero General Public License v3.0 | 5 votes |
def toString(self, noItemData=True): self.header.fileLength = ( self.header._FORMAT.size + self.header.itemCount * HuaweiFirmwareItem._FORMAT.size - 0x4c # FIXME: Can not find where does this bias come from. ) strs = [ self.header.toString()[20:], # Partial header used for calculate CRC32 value. ] if self.header.extraHeaderLength: strs.append(self.extraHeader) self.header.fileLength += len(self.extraHeader) data = [] for item in self.items: strs.append(item.toString()) data.append(item.data) self.header.fileLength += item.size # Convert to big endian. self.header.fileLength = socket.htonl(self.header.fileLength) # Update header CRC32 value. self.header.headerCrc = seqCrc32(strs) if not noItemData: strs.extend(data) # All data are present, now update file CRC32 value. strs[0] = self.header.toString()[12:] self.header.fileCrc = seqCrc32(strs) # Using the latest header with correct CRC32 value and file length. strs[0] = self.header.toString() return ''.join(strs)
Example #8
Source File: state_update.py From TupleNet with Apache License 2.0 | 5 votes |
def int_to_ip(ip_int): return socket.inet_ntoa(struct.pack('I',socket.htonl(ip_int)))
Example #9
Source File: communication.py From Software-Architecture-with-Python with MIT License | 5 votes |
def send(channel, *args): """ Send a message to a channel """ buf = pickle.dumps(args) value = socket.htonl(len(buf)) size = struct.pack("L",value) channel.send(size) channel.send(buf)
Example #10
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34)
Example #11
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #12
Source File: test_socket.py From medicare-demo with Apache License 2.0 | 5 votes |
def testNtoH(self): if sys.platform[:4] == 'java': return # problems with int & long # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #13
Source File: 1_5_integer_conversion.py From Python-Network-Programming-Cookbook-Second-Edition with MIT License | 5 votes |
def convert_integer(): data = 1234 # 32-bit print ("Original: %s => Long host byte order: %s, Network byte order: %s" %(data, socket.ntohl(data), socket.htonl(data))) # 16-bit print ("Original: %s => Short host byte order: %s, Network byte order: %s" %(data, socket.ntohs(data), socket.htons(data)))
Example #14
Source File: WXBizMsgCrypt_py3.py From TaskBot with GNU General Public License v3.0 | 5 votes |
def encrypt(self, text, appid): """对明文进行加密 @param text: 需要加密的明文 @return: 加密得到的字符串 """ # 16位随机字符串添加到明文开头 len_str = struct.pack("I", socket.htonl(len(text.encode()))) # text = self.get_random_str() + binascii.b2a_hex(len_str).decode() + text + appid text = self.get_random_str() + len_str + text.encode() + appid # 使用自定义的填充方式对明文进行补位填充 pkcs7 = PKCS7Encoder() text = pkcs7.encode(text) # 加密 cryptor = AES.new(self.key, self.mode, self.key[:16]) try: ciphertext = cryptor.encrypt(text) # 使用BASE64对加密后的字符串进行编码 return ierror.WXBizMsgCrypt_OK, base64.b64encode(ciphertext).decode('utf8') except Exception as e: return ierror.WXBizMsgCrypt_EncryptAES_Error, None
Example #15
Source File: WEGOBizMsgCrypt.py From wego with Apache License 2.0 | 5 votes |
def encrypt(self, xml): """对明文进行加密 @param text: 需要加密的明文 @return: 加密得到的字符串 """ # 16位随机字符串添加到明文开头 xml = xml.encode('utf-8') if PY2: text = self.get_random_str() +\ struct.pack("I", socket.htonl(len(xml))) + xml + self.appid else: text = self.get_random_str().encode('utf-8') +\ struct.pack("I", socket.htonl(len(xml))) +\ xml + self.appid.encode('utf-8') # 使用自定义的填充方式对明文进行补位填充 pkcs7 = PKCS7Encoder() text = pkcs7.encode(text) # 加密 cryptor = AES.new(self.key, self.mode, self.key[:16]) try: ciphertext = cryptor.encrypt(text) # 使用BASE64对加密后的字符串进行编码 result = base64.b64encode(ciphertext).decode("utf8") return Crypt_OK, result except Exception: return Crypt_EncryptAES_Error, None
Example #16
Source File: test_socket.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): if sys.platform[:4] == 'java': return # problems with int & long # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #17
Source File: networkmanayer.py From my-weather-indicator with MIT License | 5 votes |
def route_to_dbus(route, family): addr, netmask, gateway, metric = route return [ fixups.addr_to_dbus(addr, family), fixups.mask_to_dbus(netmask), fixups.addr_to_dbus(gateway, family), socket.htonl(metric) ] # Constants below are generated with makeconstants.py. Do not edit manually.
Example #18
Source File: 12_3_chat_server_with_select.py From Python-Network-Programming with MIT License | 5 votes |
def send(channel, *args): buffer = pickle.dumps(args) value = socket.htonl(len(buffer)) size = struct.pack("L",value) channel.send(size) channel.send(buffer)
Example #19
Source File: 11_5_integer_conversion.py From Python-Network-Programming with MIT License | 5 votes |
def convert_integer(): data = 1234 # 32-bit print ("Original: %s => Long host byte order: %s, Network byte order: %s" %(data, socket.ntohl(data), socket.htonl(data))) # 16-bit print ("Original: %s => Short host byte order: %s, Network byte order: %s" %(data, socket.ntohs(data), socket.htons(data)))
Example #20
Source File: base.py From dingtalk-sdk with GNU General Public License v3.0 | 5 votes |
def _encrypt(self, text, _id): text = to_binary(text) tmp_list = [] tmp_list.append(to_binary(self.get_random_string())) length = struct.pack(b'I', socket.htonl(len(text))) tmp_list.append(length) tmp_list.append(text) tmp_list.append(to_binary(_id)) text = b''.join(tmp_list) text = PKCS7Encoder.encode(text) ciphertext = to_binary(self.cipher.encrypt(text)) return base64.b64encode(ciphertext)
Example #21
Source File: test_socket.py From CTFCrackTools with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): if sys.platform[:4] == 'java': return # problems with int & long # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #22
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1, 2, 3 ] bad_values = [ -1, -2, -3, -1, -2, -3 ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #23
Source File: test_socket.py From oss-ftp with MIT License | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #24
Source File: test_socket.py From oss-ftp with MIT License | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1L, 2L, 3L ] bad_values = [ -1, -2, -3, -1L, -2L, -3L ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #25
Source File: test_socket.py From BinderFilter with MIT License | 5 votes |
def testNtoHErrors(self): good_values = [ 1, 2, 3, 1L, 2L, 3L ] bad_values = [ -1, -2, -3, -1L, -2L, -3L ] for k in good_values: socket.ntohl(k) socket.ntohs(k) socket.htonl(k) socket.htons(k) for k in bad_values: self.assertRaises(OverflowError, socket.ntohl, k) self.assertRaises(OverflowError, socket.ntohs, k) self.assertRaises(OverflowError, socket.htonl, k) self.assertRaises(OverflowError, socket.htons, k)
Example #26
Source File: test_socket.py From BinderFilter with MIT License | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1L<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1L<<34)
Example #27
Source File: base.py From wechat-python-sdk with BSD 2-Clause "Simplified" License | 5 votes |
def encrypt(self, text, appid): """对明文进行加密 @param text: 需要加密的明文 @return: 加密得到的字符串 """ # 16位随机字符串添加到明文开头 text = self.get_random_str() + struct.pack("I", socket.htonl(len(text))) + to_binary(text) + appid # 使用自定义的填充方式对明文进行补位填充 pkcs7 = PKCS7Encoder() text = pkcs7.encode(text) # 加密 cryptor = AES.new(self.key, self.mode, self.key[:16]) try: ciphertext = cryptor.encrypt(text) # 使用BASE64对加密后的字符串进行编码 return base64.b64encode(ciphertext) except Exception as e: raise EncryptAESError(e)
Example #28
Source File: utils_net.py From avocado-vt with GNU General Public License v2.0 | 5 votes |
def set_netmask(self, netmask): """ Set netmask """ if not CTYPES_SUPPORT: raise exceptions.TestSkipError("Setting the netmask requires " "python > 2.4") netmask = ctypes.c_uint32(~((2 ** (32 - netmask)) - 1)).value nmbytes = socket.htonl(netmask) ifreq = struct.pack('16sH2si8s', self.name.encode(), socket.AF_INET, b'\x00' * 2, nmbytes, b'\x00' * 8) fcntl.ioctl(sockfd, arch.SIOCSIFNETMASK, ifreq)
Example #29
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def testNtoH(self): # This just checks that htons etc. are their own inverse, # when looking at the lower 16 or 32 bits. sizes = {socket.htonl: 32, socket.ntohl: 32, socket.htons: 16, socket.ntohs: 16} for func, size in sizes.items(): mask = (1<<size) - 1 for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210): self.assertEqual(i & mask, func(func(i&mask)) & mask) swapped = func(mask) self.assertEqual(swapped & mask, mask) self.assertRaises(OverflowError, func, 1<<34)
Example #30
Source File: ifconfig.py From honeything with GNU General Public License v3.0 | 5 votes |
def set_netmask(self, netmask): netmask = ctypes.c_uint32(~((2 ** (32 - netmask)) - 1)).value nmbytes = socket.htonl(netmask) ifreq = struct.pack('16sH2si8s', self.name, AF_INET, '\x00'*2, nmbytes, '\x00'*8) fcntl.ioctl(sockfd, SIOCSIFNETMASK, ifreq)