如何从包含报头和主体上的响应中提取的JSON数据? [英] How to extract JSON data from a response containing a header and body?

查看:336
本文介绍了如何从包含报头和主体上的响应中提取的JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我造成堆栈溢出,因为通常我可以在这里找到解决我的问题第一个问题,但对于这种特定情况下,我不能。我写我的编译器一个Python插件,输出在与API交互各种语言的REST调用。我在JSON形式发送的请求主体用户名和密码与插座和SSL验证的模块。一旦验证成功,API返回按以下格式在体内重要的响应数据的响应:

this is my first question posed to Stack Overflow, because typically I can find the solutions to my problem here, but for this particular situation, I cannot. I am writing a Python plugin for my compiler that outputs REST calls in various languages for interaction with an API. I am authenticating with the socket and ssl modules by sending a username and password in the request body in JSON form. Upon successful authentication, the API returns a response in the following format with important response data in the body:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Date: Tue, 05 Feb 2013 03:36:18 GMT
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Accept-Ranges: bytes
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,OPTIONS,GET
Access-Control-Allow-Headers: Content-Type
Server: Restlet-Framework/2.0m5
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 94

{"authentication-token":"<token>","authentication-secret":"<secret>"} 

这可能是一个Pythonistas非常初级的问题,鉴于其强大的工具,字符串操作。但是,唉,我是一个新的程序员谁与Java启动。我想知道什么是解析这个整个响应,以获得&LT;令牌GT;最好的办法&LT;秘密&GT; ?我应该使用一个{和转储字符串搜索成JSON对象?我的直觉告诉我,试图用re模块,但我似乎无法弄清楚如何会在这种情况下使用,因为令牌和秘密的格局显然不是predictable。因为我已选择与低级别模块组来验证,这种反应是通过构造头和附加JSON数据到它在体内,然后执行请求和获得具有下列code中的反应获得的一个大的字符串

This is probably a very elementary question for Pythonistas, given its powerful tools for String manipulation. But alas, I am a new programmer who started with Java. I would like to know what would be the best way to parse this entire response to obtain the "<token>" and "<secret>"? Should I use a search for a "{" and dump the substring into a json object? My intuition is telling me to try and use the re module, but I cannot seem to figure out how it would be used in this situation, since the pattern of the token and secret are obviously not predictable. Because I have opted to authenticate with a low-level module set, this response is one big String obtained by constructing the header and appending JSON data to it in the body, then executing the request and obtaining the response with the following code:

#Socket configuration and connection execution
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn = ssl.wrap_socket(sock, ca_certs = pem_file)
conn.connect((host, port))
conn.send(req)

response = conn.recv()
print(response)

print语句输出第一code样品。任何帮助或洞察力会的极大地的AP preciated!

The print statement outputs the first code sample. Any help or insight would be greatly appreciated!

推荐答案

HTTP头从身体的其余部分分割 \\ r \\ n \\ r \\ n 序列。做这样的事情:

HTTP headers are split from the rest of the body by a \r\n\r\n sequence. Do something like:

import json

...

(headers, js) = response.split("\r\n\r\n")
data = json.loads(js)
token = data["authentication-token"]
secret = data["authentication-secret"]

您可能会想检查响应等,以及各种库(例如请求)能为你做这一切轻松许多。

You'll probably want to check the response, etc, and various libraries (e.g. requests) can do all of this a whole lot easier for you.

这篇关于如何从包含报头和主体上的响应中提取的JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆