发布请求不会发送

时间:2019-06-03 07:58:50

标签: python json python-3.x python-requests server-sent-events

你好。我正在尝试从eventStream中读取"line"的数据,并将该数据用于请求的有效负载中。 我已将{line}添加到有效载荷中,希望它可以工作,但是没有运气。

我的代码如下所示

import json
import requests
from sseclient import SSEClient as EventSource
url = "http://colorillo.com/draw.php"

querystring = {"ing":"_index"}

payload = {'l': '{line}', 'c': 'ffffff', 'w': '100', 'o': '100', 'f': '1', '_': 'false'}

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': "colorillo.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"
}




url1 = 'http://colorillo.com/_watch//_index?_=1559545075281'
for event in EventSource(url1):
if event.event == 'message':
    try:
        change = json.loads(event.data)
    except ValueError:
        pass
    else:
        print('{ident} drew {status}'.format(**change))
        requests.request("POST", url, data=payload, headers=headers, params=querystring)

1 个答案:

答案 0 :(得分:0)

好吧,当您要发出请求时,您不应该发出

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

正确的方法是

requestx.post(url, data = payload, params = querystring, headers = headers)

我认为那是正确的方法。

测试并告诉我们结果。

检查代码后,您会遇到很多错误,甚至会遇到缩进错误

import json
import requests
from sseclient import SSEClient as EventSource
url = "http://colorillo.com/draw.php"

querystring = {"ing":"_index"}

payload = {'l': '{line}', 'c': 'ffffff', 'w': '100', 'o': '100', 'f': '1', '_': 'false'}

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': "colorillo.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"
}




url1 = 'http://colorillo.com/_watch//_index?_=1559545075281'
for event in EventSource(url1):
if event.event == 'message': #indent error
    try:
        change = json.loads(event.data)
    except ValueError:
        pass
    else: #where is the if ?
        print('{ident} drew {status}'.format(**change))
        requests.request("POST", url, data=payload, headers=headers, params=querystring) # the post request is wrong

这应该工作

import json
import requests
from sseclient import SSEClient as EventSource
url = "http://colorillo.com/draw.php"

querystring = {"ing":"_index"}

payload = {'l': '{line}', 'c': 'ffffff', 'w': '100', 'o': '100', 'f': '1', '_': 'false'}

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': "colorillo.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"
}




url1 = 'http://colorillo.com/_watch//_index?_=1559545075281'
for event in EventSource(url1):
    if event.event == 'message':
        try:
            change = json.loads(event.data)
        except ValueError:
            pass
    else:
        print('{ident} drew {status}'.format(**change))
        requests.post(url, data=payload, headers=headers, params=querystring)

我只是检查了基本的代码语法和基础知识,我没有进行测试,我修复了常见错误,其他都是与请求本身有关的错误