Python3 and Python2 codes cause hmac.new/base64.b64encode (' value ') python3 various errors

Source: Internet
Author: User
Tags base64 hmac sha1

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.