当前位置: 首页>>代码示例>>Python>>正文


Python socket.htonl方法代码示例

本文整理汇总了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 
开发者ID:EvilPsyCHo,项目名称:TaskBot,代码行数:22,代码来源:WXBizMsgCrypt_py3.py

示例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 
开发者ID:ScottAI,项目名称:-Odoo---,代码行数:21,代码来源:WXBizMsgCrypt.py

示例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) 
开发者ID:doraemonext,项目名称:wechat-python-sdk,代码行数:21,代码来源:base.py

示例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 
开发者ID:vipshop,项目名称:TupleNet,代码行数:25,代码来源:pkt-trace.py

示例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 
开发者ID:chenjianjx,项目名称:wechat-encrypt-python3,代码行数:23,代码来源:WXBizMsgCrypt.py

示例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) 
开发者ID:wechatpy,项目名称:wechatpy,代码行数:16,代码来源:base.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_socket.py

示例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) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:15,代码来源:test_socket.py

示例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) 
开发者ID:avocado-framework,项目名称:avocado-vt,代码行数:14,代码来源:utils_net.py

示例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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:15,代码来源:test_socket.py

示例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) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:15,代码来源:test_socket.py

示例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) 
开发者ID:omererdem,项目名称:honeything,代码行数:7,代码来源:ifconfig.py

示例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) 
开发者ID:LeeXiaolan,项目名称:hwfw-tool,代码行数:34,代码来源:hwfw.py

示例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))) 
开发者ID:vipshop,项目名称:TupleNet,代码行数:4,代码来源:state_update.py

示例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) 
开发者ID:PacktPublishing,项目名称:Software-Architecture-with-Python,代码行数:10,代码来源:communication.py


注:本文中的socket.htonl方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。