python如何实现get传body_Python charset.get_body_encoding方法代码示例

# 需要导入模块: from email import charset [as 别名]

# 或者: from email.charset import get_body_encoding [as 别名]

def set_charset(self, charset):

"""Set the charset of the payload to a given character set.

charset can be a Charset instance, a string naming a character set, or

None. If it is a string it will be converted to a Charset instance.

If charset is None, the charset parameter will be removed from the

Content-Type field. Anything else will generate a TypeError.

The message will be assumed to be of type text/* encoded with

charset.input_charset. It will be converted to charset.output_charset

and encoded properly, if needed, when generating the plain text

representation of the message. MIME headers (MIME-Version,

Content-Type, Content-Transfer-Encoding) will be added as needed.

"""

if charset is None:

self.del_param('charset')

self._charset = None

return

if isinstance(charset, basestring):

charset = email.charset.Charset(charset)

if not isinstance(charset, email.charset.Charset):

raise TypeError(charset)

# BAW: should we accept strings that can serve as arguments to the

# Charset constructor?

self._charset = charset

if 'MIME-Version' not in self:

self.add_header('MIME-Version', '1.0')

if 'Content-Type' not in self:

self.add_header('Content-Type', 'text/plain',

charset=charset.get_output_charset())

else:

self.set_param('charset', charset.get_output_charset())

if isinstance(self._payload, unicode):

self._payload = self._payload.encode(charset.output_charset)

if str(charset) != charset.get_output_charset():

self._payload = charset.body_encode(self._payload)

if 'Content-Transfer-Encoding' not in self:

cte = charset.get_body_encoding()

try:

cte(self)

except TypeError:

self._payload = charset.body_encode(self._payload)

self.add_header('Content-Transfer-Encoding', cte)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值