PYTHON3 encoded please view this article: https://www.cnblogs.com/575dsj/p/7112767.html
The first time: Python3 is that bytes cannot be str. All right, recognize it. I'm going to preach bytes.
b= hmac.new ('/admindevice/getcamerasetting ', ' Adbaskjclas ', SHA1). Hexdigest ()
Print (b)
--------------------------------------------------------------------------------------------
Traceback (most recent):
File "d:/py3_study/run_test/md_code.py", line at <module>
b= hmac.new ('/admindevice/getcamerasetting ', ' Adbaskjclas ', SHA1). Hexdigest ()
File "F:\Python36\lib\hmac.py", line 144, in new
Return HMAC (Key, MSG, DIGESTMOD)
File "F:\Python36\lib\hmac.py", line at __init__
Raise TypeError ("key:expected bytes or ByteArray, but got%r"% type (key). __name__)
TypeError:key:expected bytes or ByteArray, but got ' str '
Came the second time: Read the newspaper wrong, sad urge it. Hmac.new said it had to be a string;
Because Hmac.new is a code that requires utf-8.
b= hmac.new (bytes ('/admindevice/getcamerasetting '), bytes (' Adbaskjclas '), SHA1). Hexdigest ()
Print (b)
----------------------------------------------------------------
Traceback (most recent):
File "d:/py3_study/run_test/md_code.py", line at <module>
b= hmac.new (bytes ('/admindevice/getcamerasetting '), bytes (' Adbaskjclas '), SHA1). Hexdigest ()
typeerror:string argument without an encoding
Well, Hamc.new's request is UTF-8, I'll turn UTF-8, and finally succeed.
b= hmac.new (bytes ('/admindevice/getcamerasetting ', ' utf-8 '), bytes (' Adbaskjclas ', ' utf-8 '), SHA1). Hexdigest ()
Print (b)
-------------
Results: 792A09AD139522FE77771BC5CB5FBB44B44B40B3
This approach can also be used to turn all of them into Unicode
A= hmac.new (bytes ('/admindevice/getcamerasetting ', ' latin-1 '), bytes (' Adbaskjclas ', ' latin-1 '), SHA1). Hexdigest ()
Print (a)
-------------
Results: 792A09AD139522FE77771BC5CB5FBB44B44B40B3
Base64.b64encode (' value ') is the same problem, need to pass bytes, and then back to UTF-8, disgusting enough
Reason:
Python 2 silently hides the byte-to-Unicode conversion, so long as the data is all ASCII, all conversions are correct, and once a non-ASCII character sneaks into your program, the default decoding will be invalidated, resulting in unicodedecodeerror The error. The PY2 encoding makes it easier to process ASCII. The price of your comeback is that it will fail when dealing with non-ASCII.
PY3 also has two data types: str and bytes; str type, and the Bytse type is stored in Unicode data. Bytes Data
Python3 and Python2 codes cause hmac.new/base64.b64encode (' value ') python3 various errors