本文整理汇总了Python中socket.htonl方法的典型用法代码示例。如果您正苦于以下问题:Python socket.htonl方法的具体用法?Python socket.htonl怎么用?Python socket.htonl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.htonl方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encrypt
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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
示例2: encrypt
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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
示例3: encrypt
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例4: parse_trace_path
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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
示例5: encrypt
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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
示例6: _encrypt
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例7: testNtoH
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例8: testNtoHErrors
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例9: set_netmask
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例10: testNtoH
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例11: testNtoHErrors
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例12: set_netmask
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例13: toString
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)
示例14: int_to_ip
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
def int_to_ip(ip_int):
return socket.inet_ntoa(struct.pack('I',socket.htonl(ip_int)))
示例15: send
# 需要导入模块: import socket [as 别名]
# 或者: from socket import htonl [as 别名]
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)