GVKun编程网logo

Python3和hmac。如何处理不是二进制的字符串(python不用函数求二进制)

8

在本文中,我们将详细介绍Python3和hmac。如何处理不是二进制的字符串的各个方面,并为您提供关于python不用函数求二进制的相关解答,同时,我们也将为您带来关于c–为什么EXE不是二进制的?、

在本文中,我们将详细介绍Python3和hmac。如何处理不是二进制的字符串的各个方面,并为您提供关于python不用函数求二进制的相关解答,同时,我们也将为您带来关于c – 为什么EXE不是二进制的?、MAC 如何在安装anaconda的同时,安装python3和python2、Python 16进制与字符串的转换、二进制 to 十进制、十六进制 to 十进制、十进制 to 二进制、python gzipped fileinput返回二进制字符串而不是文本字符串的有用知识。

本文目录一览:

Python3和hmac。如何处理不是二进制的字符串(python不用函数求二进制)

Python3和hmac。如何处理不是二进制的字符串(python不用函数求二进制)

我在Python2中有个脚本,效果很好。

def _generate_signature(data):   return hmac.new(''key'', data, hashlib.sha256).hexdigest()

数据是的输出json.dumps

现在,如果我尝试在Python 3中运行相同类型的代码,则会得到以下信息:

Traceback (most recent call last):  File "<stdin>", line 1, in <module>  File "/usr/lib/python3.4/hmac.py", line 144, in new    return HMAC(key, msg, digestmod)  File "/usr/lib/python3.4/hmac.py", line 42, in __init__    raise TypeError("key: expected bytes or bytearray, but got %r" %type(key).__name__)TypeError: key: expected bytes or bytearray, but got ''str''

如果我尝试将密钥转换为字节这样的操作:

bytes(''key'')

我懂了

Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: string argument without an encoding

我仍在努力理解Python 3中的编码。

答案1

小编典典

您可以使用字节字面量: b''key''

def _generate_signature(data):    return hmac.new(b''key'', data, hashlib.sha256).hexdigest()

除此之外,请确保data也是字节。例如,如果从文件中读取文件,则在打开文件时需要使用binary模式(rb)。

c – 为什么EXE不是二进制的?

c – 为什么EXE不是二进制的?

为什么如果你在十六进制编辑器中打开一个EXE,你会看到各种各样的事情.如果计算机只能理解二进制文件,那么文件中只能看到2个可能的符号?谢谢

解决方法

十六进制值在内存中被解释为二进制值.该软件只能让人更加可读.

0000 = 00001 = 10010 = 20011 = 30100 = 40101 = 50110 = 60111 = 71000 = 81001 = 91010 = 10 A1011 = 11 B1100 = 12℃1101 = 13 D1110 = 14 E1111 = 15 F

MAC 如何在安装anaconda的同时,安装python3和python2

MAC 如何在安装anaconda的同时,安装python3和python2

 

 

最近因为研究需要,在MAC的操作系统中需要同时安装python2和python3;最开始mac系统已经安装anaconda并且默认自带python3,如图所示输入python,可以查看当前python版本 

输入以下指令可以查看有哪些安装包

$ pip list

如果想同时安装python2,只需要重新配置环境变量,以安装python2.7为例子,指令如下

conda create --name python27 python=2.7

这样就把2.7版本的python命名为python27,看到如下界面,表示安装正在进行,如果没有特殊要求,可以一路点击默认安装,它会安装在anaconda3下envs文件夹下,命名就是python27

 

当出现如下界面时表示已经安装完成

 

这时如果想从python3切换到python2,可以输入

$ conda activate python27

 可以看到环境已经切换

如果想换回python3,可以输入

$ conda deactivate

可以看到环境已经回退到python3,这样是不是很方便 

 

Python 16进制与字符串的转换、二进制 to 十进制、十六进制 to 十进制、十进制 to 二进制

Python 16进制与字符串的转换、二进制 to 十进制、十六进制 to 十进制、十进制 to 二进制

2.7版本

2.7版本下进行转换还是很方便的,hex2char:output = 'data'.decode('hex')

                char2hex: output = '64617461'.encode('hex')

import sys

choose = sys.argv[1]
data = sys.argv[2]

def hex2char():
    output = data.decode('hex')
    print output
  
def char2hex():
    output = data.encode('hex')
    print output

print "Usage:  <filename> <hex2char or char2hex> <your data>"

if len(sys.argv) == 3:
    if choose.lower() == 'hex2char':
        hex2char()
       
    if choose.lower() == 'char2hex':
        char2hex()
    
    if choose.lower()!='hex2char' and choose.lower()!='char2hex':
        print "Wrong param,try again"
else:
    print "Wrong number of params,check your input\n"

#this script has passed the test

3.0版本

3.0环境比较常用的是binascii模块,关于这个模块的一些函数和方法可以查找手册,这里且说对于十六进制和字符串的转换

def hex2char(data):
#    binascii.a2b_hex(hexstr) 
    output = binascii.unhexlify(data)
    print(output)

def char2hex(data):
    data = b'data'
#    binascii.b2a_hex(data) 
    output = binascii.hexlify(data)
    print(output)

二进制 to 十进制

def bin2dec(string_num):
    return str(int(string_num,2))

十六进制 to 十进制

def hex2dec(string_num):
    return str(int(string_num.upper(),16))

十进制 to 二进制

def dec2bin(string_num):
    num = int(string_num)
    mid = []
    while True:
        if num == 0: break
        num,rem = divmod(num,2)
        mid.append(base[rem])

    return ''.join([str(x) for x in mid[::-1]])

# 获取用户输入十进制数
dec = int(input("输入数字:"))

print("十进制数为:",dec)
print("转换为二进制为:",bin(dec))
print("转换为八进制为:",oct(dec))
print("转换为十六进制为:",hex(dec))

python gzipped fileinput返回二进制字符串而不是文本字符串

python gzipped fileinput返回二进制字符串而不是文本字符串

当我使用模块fileinput循环遍历一组gzip压缩文件的行时,如下所示:

for line in fileinput.FileInput(files=gzipped_files,openhook=fileinput.hook_compressed):

那些行是字节字符串而不是文本字符串.

当使用模块gzip时,可以通过使用’rt’而不是’rb’打开文件来防止这种情况:http://bugs.python.org/issue13989

模块fileinput是否有类似的修复,所以我可以让它返回文本字符串而不是字节字符串?我尝试添加mode =’rt’,但后来我收到此错误:

ValueError: FileInput opening mode must be one of 'r','rU','U' and 'rb'
最佳答案
您必须实现自己的openhook函数来使用编解码器打开文件:

import os

def hook_compressed_text(filename,mode,encoding='utf8'):
    ext = os.path.splitext(filename)[1]
    if ext == '.gz':
        import gzip
        return gzip.open(filename,mode + 't',encoding=encoding)
    elif ext == '.bz2':
        import bz2
        return bz2.open(filename,encoding=encoding)
    else:
        return open(filename,encoding=encoding)

关于Python3和hmac。如何处理不是二进制的字符串python不用函数求二进制的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于c – 为什么EXE不是二进制的?、MAC 如何在安装anaconda的同时,安装python3和python2、Python 16进制与字符串的转换、二进制 to 十进制、十六进制 to 十进制、十进制 to 二进制、python gzipped fileinput返回二进制字符串而不是文本字符串等相关内容,可以在本站寻找。

本文标签: