解析python中chunked的内容类型的响应

时间:2011-09-15 00:02:37

标签: python stream chunked http-1.1 atmosphere

我正在尝试阅读和解析内容类型的请求:在python中分块。这是我在浏览器中加载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"},

我想检索和解析像这样的json条目:

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

我该怎么做?

由于

1 个答案:

答案 0 :(得分:1)

“chunked”不是有效的内容类型,尽管它是有效的transfer-encoding。根据您发布的样本,这看起来不像您的问题。这看起来像应用于常规jsonp响应的标头。在许多情况下,浏览器会忽略sgml注释,但您必须手动提取它以供自己使用。以下是处理该问题的想法:

>>> 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'}