Return the charset parameter of the Content-Type header. The returned string is always coerced to lower case. If there is no Content-Type header, or if that header has no charset parameter, failobj is returned.
(self, failobj=None)
| 923 | self._headers = newheaders |
| 924 | |
| 925 | def get_content_charset(self, failobj=None): |
| 926 | """Return the charset parameter of the Content-Type header. |
| 927 | |
| 928 | The returned string is always coerced to lower case. If there is no |
| 929 | Content-Type header, or if that header has no charset parameter, |
| 930 | failobj is returned. |
| 931 | """ |
| 932 | missing = object() |
| 933 | charset = self.get_param('charset', missing) |
| 934 | if charset is missing: |
| 935 | return failobj |
| 936 | if isinstance(charset, tuple): |
| 937 | # RFC 2231 encoded, so decode it, and it better end up as ascii. |
| 938 | pcharset = charset[0] or 'us-ascii' |
| 939 | try: |
| 940 | # LookupError will be raised if the charset isn't known to |
| 941 | # Python. UnicodeError will be raised if the encoded text |
| 942 | # contains a character not in the charset. |
| 943 | as_bytes = charset[2].encode('raw-unicode-escape') |
| 944 | charset = str(as_bytes, pcharset) |
| 945 | except (LookupError, UnicodeError): |
| 946 | charset = charset[2] |
| 947 | # charset characters must be in us-ascii range |
| 948 | try: |
| 949 | charset.encode('us-ascii') |
| 950 | except UnicodeError: |
| 951 | return failobj |
| 952 | # RFC 2046, $4.1.2 says charsets are not case sensitive |
| 953 | return charset.lower() |
| 954 | |
| 955 | def get_charsets(self, failobj=None): |
| 956 | """Return a list containing the charset(s) used in this message. |