binascii --- تبدیل بین دادههای دودویی و ASCII¶
ماژول binascii شامل تعدادی متد برای تبدیل میان دادههای دودویی و بازنماییهای دودویی مختلف کدگذاریشده با ASCII است. بهطور معمول، شما این توابع را مستقیماً استفاده نمیکنید، بلکه در عوض از ماژولهای پوششی مانند base64 استفاده میکنید. ماژول binascii شامل توابع سطح پایینی است که برای سرعت بیشتر به زبان C نوشته شدهاند و توسط ماژولهای سطح بالاتر استفاده میشوند.
توجه
توابع a2b_* رشتههای یونیکدی را میپذیرند که فقط شامل نویسههای ASCII باشند. سایر توابع فقط اشیاء شبهبایت (مانند bytes، bytearray و اشیاء دیگری که از پروتکل بافر پشتیبانی میکنند) را میپذیرند.
تغییر یافته در نسخهی 3.3: رشتههای یونیکدی که فقط شامل نویسههای ASCII هستند، اکنون توسط توابع a2b_* پذیرفته میشوند.
ماژول binascii توابع زیر را تعریف میکند:
- binascii.a2b_uu(string)¶
یک خط منفرد از دادههای uuencoded را به دادههای دودویی تبدیل میکند و دادههای دودویی را برمیگرداند. سطرها معمولاً حاوی ۴۵ بایت (دودویی) هستند، بهجز خط آخر. ممکن است پس از دادههای خط، فضای سفید وجود داشته باشد.
- binascii.b2a_uu(data, *, backtick=False)¶
دادههای دودویی را به سطری از نویسههای ASCII تبدیل میکند؛ مقدار بازگشتی، خط تبدیلشده بههمراه یک نویسه خط جدید است. طول data باید حداکثر 45 باشد. اگر backtick درست باشد، صفرها بهجای فاصلهها با
'`'نمایش داده میشوند.تغییر یافته در نسخهی 3.7: پارامتر backtick افزوده شد.
- binascii.a2b_base64(string, /, *, padded=True, alphabet=BASE64_ALPHABET, strict_mode=False, canonical=False)¶
- binascii.a2b_base64(string, /, *, ignorechars, padded=True, alphabet=BASE64_ALPHABET, strict_mode=True, canonical=False)
یک بلوک از دادههای base64 را دوباره به دودویی تبدیل میکند و دادههای دودویی را برمیگرداند. شما میتوانید بیش از یک خط را در هر بار ارسال کنید.
Optional alphabet must be a
bytesobject of length 64 which specifies an alternative alphabet.If padded is true, the last group of 4 base 64 alphabet characters must be padded with the '=' character. If padded is false, padding is neither required nor recognized: the '=' character is not treated as padding but as a non-alphabet character, which means it is silently discarded when strict_mode is false, or causes an
Errorwhen strict_mode is true unless b'=' is included in ignorechars.If ignorechars is specified, it should be a bytes-like object containing characters to ignore from the input when strict_mode is true. If ignorechars contains the pad character
'=', the pad characters presented before the end of the encoded data and the excess pad characters will be ignored. The default value of strict_mode isTrueif ignorechars is specified,Falseotherwise.اگر strict_mode درست باشد، فقط دادههای معتبر base64 تبدیل میشوند. دادههای نامعتبر base64 باعث پرتاب
binascii.Errorمیشوند.base64 معتبر:
Conforms to RFC 4648.
فقط شامل نویسههای الفبای base64 است.
فاقد داده اضافی پس از پدینگ (padding) است (از جمله پدینگ اضافی، سطرهای جدید و غیره).
با یک پرکننده (padding) شروع نمیشود.
If canonical is true, non-zero padding bits in the last group are rejected with
binascii.Error, enforcing canonical encoding as defined in RFC 4648 section 3.5. This check is independent of strict_mode.تغییر یافته در نسخهی 3.11: پارامتر strict_mode اضافه شد.
تغییر یافته در نسخهی 3.15: Added the alphabet, canonical, ignorechars, and padded parameters.
- binascii.b2a_base64(data, *, padded=True, alphabet=BASE64_ALPHABET, wrapcol=0, newline=True)¶
Convert binary data to a line(s) of ASCII characters in base64 coding, as specified in RFC 4648.
If padded is true (default), pad the encoded data with the '=' character to a size multiple of 4. If padded is false, do not add the pad characters.
If wrapcol is non-zero, insert a newline (
b'\n') character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.If newline is true (default), a newline character will be added at the end of the output.
تغییر یافته در نسخهی 3.6: پارامتر newline افزوده شد.
تغییر یافته در نسخهی 3.15: Added the alphabet, padded and wrapcol parameters.
- binascii.a2b_ascii85(string, /, *, foldspaces=False, adobe=False, ignorechars=b'', canonical=False)¶
Convert Ascii85 data back to binary and return the binary data.
Valid Ascii85 data contains characters from the Ascii85 alphabet in groups of five (except for the final group, which may have from two to five characters). Each group encodes 32 bits of binary data in the range from
0to2 ** 32 - 1, inclusive. The special characterzis accepted as a short form of the group!!!!!, which encodes four consecutive null bytes. A single-character final group is always rejected as an encoding violation.foldspaces is a flag that specifies whether the 'y' short sequence should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20). This feature is not supported by the "standard" Ascii85 encoding.
adobe controls whether the encoded byte sequence is framed with
<~and~>, as in a PostScript base-85 string literal. If adobe is true, a leading<~is optionally accepted, while a trailing~>is required, andbinascii.Erroris raised if it is not found.ignorechars should be a bytes-like object containing characters to ignore from the input. This should only contain whitespace characters.
If canonical is true, non-canonical encodings are rejected with
binascii.Error. Here "canonical" means the encoding thatb2a_ascii85()would produce: thezabbreviation must be used for all-zero groups (rather than!!!!!), and partial final groups must use the same padding digits as the encoder.Invalid Ascii85 data will raise
binascii.Error.اضافه شده در نسخهی 3.15.
- binascii.b2a_ascii85(data, /, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)¶
Convert binary data to a formatted sequence of ASCII characters in Ascii85 coding. The return value is the converted data.
foldspaces is an optional flag that uses the special short sequence 'y' instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This feature is not supported by the "standard" Ascii85 encoding.
If wrapcol is non-zero, insert a newline (
b'\n') character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.If pad is true, the zero-padding applied to the end of the input is fully retained in the output encoding, as done by
btoa, producing an exact multiple of 5 bytes of output. This is not part of the standard encoding used in PDF, as it does not preserve the length of the data.adobe controls whether the encoded byte sequence is framed with
<~and~>, as in a PostScript base-85 string literal. Note that while ASCII85Decode streams in PDF documents must be terminated with~>, they must not use a leading<~.اضافه شده در نسخهی 3.15.
- binascii.a2b_base85(string, /, *, alphabet=BASE85_ALPHABET, ignorechars=b'', canonical=False)¶
Convert Base85 data back to binary and return the binary data. More than one line may be passed at a time.
Valid Base85 data contains characters from the Base85 alphabet in groups of five (except for the final group, which may have from two to five characters). Each group encodes 32 bits of binary data in the range from
0to2 ** 32 - 1, inclusive. A single-character final group is always rejected as an encoding violation.Optional alphabet must be a
bytesobject of length 85 which specifies an alternative alphabet.ignorechars should be a bytes-like object containing characters to ignore from the input.
If canonical is true, non-canonical encodings are rejected with
binascii.Error. Here "canonical" means the encoding thatb2a_base85()would produce: partial final groups must use the same padding digits as the encoder.Invalid Base85 data will raise
binascii.Error.اضافه شده در نسخهی 3.15.
- binascii.b2a_base85(data, /, *, alphabet=BASE85_ALPHABET, wrapcol=0, pad=False)¶
Convert binary data to a line of ASCII characters in Base85 coding. The return value is the converted line.
Optional alphabet must be a bytes-like object of length 85 which specifies an alternative alphabet.
If wrapcol is non-zero, insert a newline (
b'\n') character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.If pad is true, the zero-padding applied to the end of the input is retained in the output, which will always be a multiple of 5 bytes, and thus the length of the data may not be preserved on decoding.
اضافه شده در نسخهی 3.15.
- binascii.a2b_base32(string, /, *, padded=True, alphabet=BASE32_ALPHABET, ignorechars=b'', canonical=False)¶
Convert base32 data back to binary and return the binary data.
Valid base32 data contains characters from the base32 alphabet specified in RFC 4648 in groups of eight (if necessary, the final group is padded to eight characters with
=). Each group encodes 40 bits of binary data in the range from0to2 ** 40 - 1, inclusive.توجه
This function does not map lowercase characters (which are invalid in standard base32) to their uppercase counterparts, nor does it contextually map
0toOand1toI/Las RFC 4648 allows.Optional alphabet must be a
bytesobject of length 32 which specifies an alternative alphabet.If padded is true, the last group of 8 base 32 alphabet characters must be padded with the '=' character. If padded is false, the '=' character is treated as other non-alphabet characters (depending on the value of ignorechars).
ignorechars should be a bytes-like object containing characters to ignore from the input. If ignorechars contains the pad character
'=', the pad characters presented before the end of the encoded data and the excess pad characters will be ignored.If canonical is true, non-zero padding bits in the last group are rejected with
binascii.Error, enforcing canonical encoding as defined in RFC 4648 section 3.5.Invalid base32 data will raise
binascii.Error.اضافه شده در نسخهی 3.15.
- binascii.b2a_base32(data, /, *, padded=True, alphabet=BASE32_ALPHABET, wrapcol=0)¶
Convert binary data to a line of ASCII characters in base32 coding, as specified in RFC 4648. The return value is the converted line.
Optional alphabet must be a bytes-like object of length 32 which specifies an alternative alphabet.
If padded is true (default), pad the encoded data with the '=' character to a size multiple of 8. If padded is false, do not add the pad characters.
If wrapcol is non-zero, insert a newline (
b'\n') character after at most every wrapcol characters. If wrapcol is zero (default), do not insert any newlines.اضافه شده در نسخهی 3.15.
- binascii.a2b_qp(data, header=False)¶
یک بلوک از دادههای quoted-printable را دوباره به دودویی تبدیل میکند و دادههای دودویی را برمیگرداند. میتوان بیش از یک خط را در هر نوبت ارسال کرد. اگر آرگومان اختیاری header وجود داشته باشد و مقدار آن درست باشد، زیرسطرها بهعنوان فاصله کدگشایی میشوند.
- binascii.b2a_qp(data, quotetabs=False, istext=True, header=False)¶
دادههای دودویی را به یک یا چند خط از نویسههای ASCII با کدگذاری quoted-printable تبدیل میکند. مقدار بازگشتی، خط یا سطرهای تبدیلشده است. اگر آرگومان اختیاری quotetabs موجود و درست باشد، همهی تبها و فاصلهها کدگذاری خواهند شد. اگر آرگومان اختیاری istext موجود و درست باشد، نویسههای خط جدید کدگذاری نمیشوند، اما فاصلههای انتهایی کدگذاری خواهند شد. اگر آرگومان اختیاری header موجود و درست باشد، فاصلهها مطابق RFC 1522 بهصورت زیرخط کدگذاری میشوند. اگر آرگومان اختیاری header موجود و نادرست باشد، نویسههای خط جدید نیز کدگذاری خواهند شد؛ در غیر این صورت، تبدیل linefeed ممکن است جریان دادههای دودویی را خراب کند.
- binascii.crc_hqx(data, value)¶
مقدار CRC ۱۶بیتی data را، با شروع از value بهعنوان CRC اولیه، محاسبه میکند و نتیجه را برمیگرداند. این از چندجملهای CRC-CCITT x16 + x12 + x5 + 1 استفاده میکند، که اغلب بهصورت 0x1021 نمایش داده میشود. این CRC در قالب binhex4 استفاده میشود.
- binascii.crc32(data[, value])¶
CRC-32، جمعآزمای ۳۲ بیتی بدون علامت برای data را با شروع از مقدار اولیهی CRC برابر با value محاسبه کنید. مقدار اولیهی پیشفرض CRC صفر است. این الگوریتم با جمعآزمای پرونده ZIP سازگار است. از آنجا که این الگوریتم برای استفاده بهعنوان الگوریتم جمعآزما طراحی شده است، برای استفاده بهعنوان یک الگوریتم هش عمومی مناسب نیست. بهصورت زیر استفاده کنید:
print(binascii.crc32(b"hello world")) # Or, in two pieces: crc = binascii.crc32(b"hello") crc = binascii.crc32(b" world", crc) print('crc32 = {:#010x}'.format(crc))
تغییر یافته در نسخهی 3.0: نتیجه همواره بدون علامت است.
- binascii.b2a_hex(data[, sep[, bytes_per_sep=1]])¶
- binascii.hexlify(data[, sep[, bytes_per_sep=1]])¶
بازنمایی مبنای شانزدهی دادهی دودویی data را برمیگرداند. هر بایت از data به بازنمایی مبنای شانزدهی ۲رقمی متناظر تبدیل میشود. بنابراین، شیء bytes برگرداندهشده دو برابر طول data طول دارد.
قابلیت مشابهی (اما با برگرداندن یک رشته متنی) همچنین بهراحتی با استفاده از متد
bytes.hex()قابل دسترسی است.اگر sep مشخص شده باشد، باید یک شیء str یا bytes تکنویسهای باشد. این جداکننده در خروجی پس از هر bytes_per_sep بایت ورودی درج میشود. بهطور پیشفرض، شمارش محل قرارگیری جداکننده از انتهای راست خروجی انجام میشود؛ اگر میخواهید از سمت چپ شمارش کنید، یک مقدار منفی برای bytes_per_sep ارائه دهید.
>>> import binascii >>> binascii.b2a_hex(b'\xb9\x01\xef') b'b901ef' >>> binascii.hexlify(b'\xb9\x01\xef', '-') b'b9-01-ef' >>> binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2) b'b9_01ef' >>> binascii.b2a_hex(b'\xb9\x01\xef', b' ', -2) b'b901 ef'
تغییر یافته در نسخهی 3.8: پارامترهای sep و bytes_per_sep افزوده شدند.
- binascii.a2b_hex(hexstr, *, ignorechars=b'')¶
- binascii.unhexlify(hexstr, *, ignorechars=b'')¶
دادهی دودویی نمایشدادهشده توسط رشتهی مبنای شانزده hexstr را برمیگرداند. این تابع معکوس
b2a_hex()است. hexstr باید حاوی تعداد زوجی از ارقام مبنای شانزده باشد (که میتوانند بهصورت حروف بزرگ یا کوچک باشند)، در غیر این صورت استثنایErrorپرتاب میشود.ignorechars should be a bytes-like object containing characters to ignore from the input.
قابلیت مشابهی (اما با سختگیری کمتر نسبت به فضای خالی) نیز از طریق متد کلاسی
bytes.fromhex()در دسترس است.تغییر یافته در نسخهی 3.15: Added the ignorechars parameter.
- exception binascii.Error¶
استثنایی که در صورت بروز خطا پرتاب میشود. این موارد معمولاً خطاهای برنامهنویسی هستند.
- exception binascii.Incomplete¶
استثنایی که هنگام ناقص بودن دادهها پرتاب میشود. این استثناها معمولاً خطاهای برنامهنویسی نیستند، اما ممکن است با خواندن اندکی داده بیشتر و تلاش دوباره مدیریت شوند.
- binascii.URLSAFE_BASE64_ALPHABET¶
The "URL and filename safe" Base 64 alphabet according to RFC 4648.
اضافه شده در نسخهی 3.15.
- binascii.UU_ALPHABET¶
The uuencoding alphabet.
اضافه شده در نسخهی 3.15.
- binascii.CRYPT_ALPHABET¶
The Base 64 alphabet used in the crypt(3) routine and in the GEDCOM format.
اضافه شده در نسخهی 3.15.
- binascii.BINHEX_ALPHABET¶
The Base 64 alphabet used in BinHex 4 (HQX) within the classic Mac OS.
اضافه شده در نسخهی 3.15.
- binascii.BASE85_ALPHABET¶
The Base85 alphabet.
اضافه شده در نسخهی 3.15.
- binascii.ASCII85_ALPHABET¶
The Ascii85 alphabet.
اضافه شده در نسخهی 3.15.