本文整理匯總了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)