UTF8问题使用Python向华为Api发送消息

时间:2019-05-06 10:42:44

标签: python api huawei

我正在使用华为调制解调器WiFi Api 这是我在Python中的代码:

from datetime import datetime

from huaweisms.api.common import post_to_url, ApiCtx, get_from_url


def send_sms(ctx: ApiCtx, dest, msg: str):

now = datetime.now()
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
dest = [dest] if isinstance(dest, str) else dest

phones_content = '\n'.join('<Phone>{}</Phone>'.format(phone) for phone in dest)
xml_data = """
    <request>
        <Index>1</Index>
        <Phones>
        {}
        </Phones>
        <Sca></Sca>
        <Content>
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        this is the test this is the test test testh test htest stes this is the test tes test for test all test
        </Content>
        <Length>300</Length>
        <Reserved>13</Reserved>
        <Date>{}</Date>
    </request>
""".format(phones_content, now_str)

headers = {
    '__RequestVerificationToken': ctx.token,
    'X-Requested-With': 'XMLHttpRequest'
}
url = "{}/sms/send-sms".format(ctx.api_base_url)
r = post_to_url(url, xml_data, ctx, headers)
return r

此代码正常工作。如您所见,我的内容有700多个字符。 但是,当我发送超过20个utf-8字符时,会收到100005错误,这表示格式不正确。 例如,代码可用于

<Content>
سلام این یک پیام است
</Content>

但相同的代码不适用于

<Content>
 سلام این یک پیام است و برای پایتون ارسال شده است
</Content>

请帮助我解决问题

0 个答案:

没有答案