【问题标题】:Python HTTPServer: How to convert the extracted post data to python dictionaryPython HTTPServer:如何将提取的帖子数据转换为python字典
【发布时间】:2021-04-15 07:07:22
【问题描述】:

嘿,所以我有一个简单的基本 python HTTPServer,现在从我发送的一些数据的发布请求中,当我提取数据时,我得到以下内容:

b'{"name":"john",  "lastname":"Doe"}'

这就是我提取给定数据的方式:

content_len = int(self.headers.get('Content-Length'))
post_body = self.rfile.read(content_len)
print(post_body)

是一种将输出转换为正确字典的方法吗?

【问题讨论】:

    标签: python http https


    【解决方案1】:

    试试这个:

    import ast
    ast.literal_eval(b'{"name":"john",  "lastname":"Doe"}'.decode("utf-8"))
    

    literal_eval 将字符串计算为其内容的 Pythonic 等效项。需要decode 才能将bytes 转换为str

    【讨论】:

      猜你喜欢
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 2018-09-12
      • 1970-01-01
      相关资源
      最近更新 更多