用python跟踪实时流媒体

2024-10-01 00:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从tradeking(https://developers.tradeking.com/documentation/node-streaming)调用实时流式api

以下是我的代码:

import requests

from requests_oauthlib import OAuth1 
CK = "CK" 
CS = "CS" 
OT = "OT" 
OS ="OS"






def read_stream():

   s = requests.Session()
   s.auth = OAuth1(CK, CS, OT, OS, signature_type='auth_header')
   symbols = ["APPL", "GOOG"]
   payload = {'symbols': ','.join(symbols)}
   headers = {'connection': 'keep-alive', 'content-type': '     application/json', 'x-powered-by': 'Express', 'transfer-encoding': 'chunked'}

    req = s.get('https://stream.tradeking.com/v1/market/quotes.json',
                       params=payload)

    prepped = s.prepare_request(req)

    resp = s.send(prepped, stream=True)

    for line in resp.iter_lines():
       if line:
          print(line)


 read_stream()

以下是我的错误。 ChunkedEncodingError:('连接断开:不完全读取(0字节读取)',不完整读取(0字节读取))

我做错什么了?在


Tags: httpsimportcomauthreadstreamosline
1条回答
网友
1楼 · 发布于 2024-10-01 00:31:19

我认为异常是在s.get(...)中引发的,它将执行实际的httpget操作并返回一个Response对象。在

处理准备好的请求的后续行看起来是错误的,并且不会对Response对象起作用,但是由于异常,代码没有被执行。在

尝试将stream=True添加到请求中,然后迭代响应:

resp = s.get('https://stream.tradeking.com/v1/market/quotes.json', stream=True, params=payload)

for line in resp.iter_lines():
    if line:
        print(line)

相关问题 更多 >