Q:python&OPI方式调用示例:行情订阅
A:本页中主要展示开发者用户在python中通过OPI接口进行行情订阅的实现示例,即使用
URI:/Service/Subscription/。
使用范例:Python中进行行情订阅并通过分段模式进行输出
代码实现如下:
import requests
from requests.auth import HTTPBasicAuth
# 创建session
session = requests.Session()
# 输入账号密码
session.auth = HTTPBasicAuth('tuser', 'tpassw') # 使用HTTPBasicAuth进行基本认证
headers = {'Connection': 'keep-alive',
'Content-Type': 'application/json',
'TS-EVENTNAME': 'auto',
'JSON-Encode': 'gbk',
'TS-BOUNDARY': 'end'}
session.headers.update(headers)
# 设置请求URL和数据
url = "https://opi.tinysoft.com.cn/Service/Subscription/"
data ={"Type": 1, "IDs": ["SZ000001"], "SUBs": ["StockName", "date_s", "price"]}
# 发送POST请求并启用流式响应
response = session.post(url, json=data, stream=True)
# 分段读取响应内容
chunk_size = 1024 # 每次读取1024字节
print("分段数据:")
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk: # 过滤掉keep-alive的chunk
print(f"数据块: {chunk.decode('unicode_escape')}...")
print("----------Subscription over------------");
response.close()
执行结果:
