了解Python HTTP流 [英] Understanding Python HTTP streaming

查看:244
本文介绍了了解Python HTTP流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用Python和Requests访问流API。

I'm struggling to access a streaming API using Python and Requests.

API说的是:我们启用了一个流端点来请求两个报价利用持久的HTTP套接字连接交换数据。来自API的流数据包括发出Authenticated HTTP请求并保持HTTP套接字打开以持续接收数据。

What the API says: "We’ve enabled a streaming endpoint to for requesting both quote and trade data utilizing a persistent HTTP socket connection. Streaming data from the API consists of making an Authenticated HTTP request and leaving the HTTP socket open to continually receive data."

如何我一直在尝试访问数据:

How I've been trying to access the data:

s = requests.Session()
def streaming(symbols):
    url = 'https://stream.tradeking.com/v1/market/quotes.json'
    payload = {'symbols': ','.join(symbols)}
    return s.get(url, params=payload, stream=True)  
r = streaming(['AAPL', 'GOOG'])

请求文档此处显示两个感兴趣的东西:使用生成器/迭代器与分块数据一起使用,传入数据领域。对于流数据,它建议使用以下代码:

The Requests docs here show two things of interest: Use a generator/iterator for use with chunked data, passed in the data field. For streaming data, it suggests using code such as:

for line in r.iter_lines():
    print(line)

两者似乎都不行,虽然我不知道在生成器函数中放什么,因为这个例子不清楚。使用r.iter_lines(),我得到输出:b'{status:connected} {status:disconnected}'

Neither seem to work, although I've no idea what to put in the generator function, since the example is unclear. Using r.iter_lines(), I get the output: "b'{"status":"connected"}{"status":disconnected"}'"

I可以访问标头,响应是HTTP 200,但无法获取有效数据,或者找到有关如何在python中访问流式HTTP数据的明确示例。任何帮助都将受到赞赏.API建议使用Jetty for Java来保持流打开,但我不确定如何在Python中执行此操作。

I can access the headers, and the response is HTTP 200, but can't get valid data, or find clear examples on how to access streaming HTTP data in python. Any help would be appreciated. The API recommends using Jetty for Java to keep the stream open, but I'm not sure how to do this in Python.

标题:{'connection':'keep-alive','content-type': 'application / json','x-powered-by':'Express','transfer-encoding':'chunked'}

Headers: {'connection': 'keep-alive', 'content-type': 'application/json', 'x-powered-by': 'Express', 'transfer-encoding': 'chunked'}

推荐答案

不确定你是否认识到这一点,但是TradeKing没有在他们的JSON blob之间添加换行符。因此你必须使用iter_content逐字节地获取它,将该字节附加到缓冲区,尝试解码缓冲区,成功时清除缓冲区并生成结果对象。:(

Not sure if you figured this out, but TradeKing doesn't put newlines in between their JSON blobs. You thus have to use iter_content to get it byte by byte, append that byte to a buffer, try to decode the buffer, on success clear the buffer and yield the resultant object. :(

这篇关于了解Python HTTP流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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