简体   繁体   English

解析python中分块的内容类型的响应

[英]Parsing responses of content-type chunked in python

I'm trying to read and parse a request of content-type: chunked in python. 我正在尝试读取和解析内容类型的请求:在python中分块。 Here is what I see when I load the url in a browser and look at the source: 这是我在浏览器中加载URL并查看源代码时看到的内容:

<!-- ---------------------------------------------------------------- http://github.com/Atmosphere ------------------------------------------------------------------------ --> 
<!-- Welcome to the Atmosphere Framework. To work with all the browsers when suspending connection, Atmosphere must output some data to makes WebKit based browser working.--> 
<!-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------- --> 
<!-- EOD -->[{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"},  {"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam1","value":"1.5584484E9"},{"__publicationName":"dip\/acc\/LHC\/Beam\/Energy","value":"495"},

I'd like to retrieve and parse the json entries like this one: 我想像这样检索和解析json条目:

{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"}

How can I do this? 我怎样才能做到这一点?

Thanks 谢谢

"chunked" is not a valid content-type, although it is a valid transfer-encoding . 尽管“块”是有效的transfer-encoding ,但它不是有效的内容类型。 Based on the sample you've posted, that doesn't really look like your problem. 根据您发布的示例,这看起来确实不是您的问题。 This looks like a header applied to a regular jsonp response. 这看起来像是应用于常规jsonp响应的标头。 In many cases, the sgml comments would be ignored by a browser, but you'll have to extract it manually for your own use. 在许多情况下,浏览器会忽略sgml注释,但是您必须手动提取它以供自己使用。 Here's an idea of dealing with that: 这是解决这个问题的想法:

>>> import json
>>> corpus = '''<!-- ---------------------------------------------------------------- http://github.com/Atmosphere ------------------------------------------------------------------------ --> 
... <!-- Welcome to the Atmosphere Framework. To work with all the browsers when suspending connection, Atmosphere must output some data to makes WebKit based browser working.--> 
... <!-- --------------------------------------------------------------------------------------------------------------------------------------------------------------------- --> 
... <!-- EOD -->[{"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam2","value":"2.505730663333334E9"},  {"__publicationName":"dip\/acc\/LHC\/Beam\/Intensity\/Beam1","value":"1.5584484E9"},{"__publicationName":"dip\/acc\/LHC\/Beam\/Energy","value":"495"}]'''
>>> junk, data = corpus.split('<!-- EOD -->', 1)
>>> parsed = json.loads(data)
>>> for item in parsed:
...     print item
... 
{u'__publicationName': u'dip/acc/LHC/Beam/Intensity/Beam2', u'value': u'2.505730663333334E9'}
{u'__publicationName': u'dip/acc/LHC/Beam/Intensity/Beam1', u'value': u'1.5584484E9'}
{u'__publicationName': u'dip/acc/LHC/Beam/Energy', u'value': u'495'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM