requests发送unicode数据

# coding=utf-8
import requests

def cn_to_unicode(in_str, need_str=True, debug=False):
    out = []

    for s in in_str:
        # 获得该字符的数值
        val = ord(s)
        # print(val)

        # 小于0xff则为ASCII码,手动构造\u00xx格式
        if val <= 0xff:
            hex_str = hex(val).replace('0x', '').zfill(4)
            # 这里不能以unicode_escape编码,不然会自动增加一个'\\'
            res = bytes('\\u' + hex_str, encoding='utf-8')
        else:
            res = s.encode("unicode_escape")

        out.append(res)

    # 调试
    if debug:
        print(out)
        print(len(out), len(out[0]), len(out[-1]))

    # 转换为str类
    if need_str:
        out_str = ''
        for s in out:
            out_str += str(s, encoding='utf-8')
        return out_str
    else:
        return out

# 错误 {"a": "x", "b": "\\u4f60\\u597d"}  会多一个反斜杠
def sendReq1():
    proxies = {
        'http': 'http://127.0.0.1:8080',
    }
    unicode_str = cn_to_unicode('你好')
    print("sendReq1: "+unicode_str)
    data = {
        'a': 'x',
        'b': unicode_str
    }
    requests.post(url='http://localhost:9999', json=data, proxies=proxies)

# 正确 {"a":"x","b":"\u4f60\u597d"}
def sendReq2():
    proxies = {
        'http': 'http://127.0.0.1:8080',
    }
    unicode_str = cn_to_unicode('你好')
    print("sendReq2: " + unicode_str)
    data = '{"a":"x","b":"%s"}' % unicode_str
    headers = {"Content-Type": "application/json"}
    requests.post(url='http://localhost:9999', headers=headers, data=data, proxies=proxies)


if __name__ == '__main__':
    sendReq1()
    sendReq2()


posted @ 2022-01-06 15:03  depycode  阅读(156)  评论(0编辑  收藏  举报