如何从SSEclient提取某些数据并与请求一起发送

时间:2019-05-24 08:46:21

标签: python-3.x python-requests

所以我的代码如下所示。

import requests
from sseclient import SSEClient

URL = 'http://EXAMPLE.com/_watch//_index?_=1558685266329'

messages = SSEClient(URL)

for msg in messages:
print(msg)      # Prints the data from the eventstream.

url = "http://EXAMPLE.com/draw.php"

querystring = {"ing":"_index"}

payload = {'l': 'HERE WOULD BE THE DATA EXTRACTED FROM THE STREAM EVENT!!'}   #With each new data event being sent, so basically loop.
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Content-Type': "application/x-www-form-urlencoded",
'X-Requested-With': "XMLHttpRequest",
'Accept': "*/*",
'User-Agent': "PostmanRuntime/7.13.0",
'Cache-Control': "no-cache",
'Postman-Token': "a4cccbb2-ec51-4694-b198-e733f1f10e4c,ed3c968e-b8b2-4168-abef-51025096933d",
'Host': "EXAMPLE.com",
'cookie': "i=cloyfj1bio8uvxlqrkfkczqxo1pmx7m5; o=100; w=15; c=t44q; h=_-g00000____5aU00bH_GqCFXg3g_SY0gtx1J808RNApYLbO6g41X1wo____T000R01Puw3rMVU0t44q7w3F0afp4NcjXz00; a=lnpe0l; oi=qld8gxkfrzalpvqgydze7dzbm8p1r2zp",
'accept-encoding': "gzip, deflate",
'content-length': "713",
'Connection': "keep-alive",
'cache-control': "no-cache"
}

response = requests.request("POST", url, data=payload, headers=headers, 
params=querystring)

print(response.text)

这可以很好地打印事件数据。问题在于它会在每个事件中打印所有数据。下面显示的是数据事件的示例。

{"ident":"bz4sp4puawiwtbspu8j1zqzwbxtyb5jp","finished":true,"line":[[247,200]],"lineColor":"380a0a","lineWidth":"200","opacity":"100","status":"f","channel":"_index"}

我只想简单地仅从“行”复制数据。

从“行”获取数据后,我想使用Python请求,并将从“行”中提取的数据用作有效载荷键和值。关键字是“ l”,值是从事件流中提取的数据。  我想让它与每个新事件数据一起循环。

因此它将从事件流中复制“行”数据,即[[247,200]],并将该数据作为有效载荷发送,因为键为“ l”,并且该值将是从事件流中提取的数据

如果我没有任何意义,请通知我再解释一下。

0 个答案:

没有答案