【问题标题】:Post request won't send发帖请求不会发送
【发布时间】:2019-10-18 18:22:17
【问题描述】:

你好。我正在尝试从 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)

【问题讨论】:

  • POST 请求在哪里?
  • 尝试在except 子句中打印并检查它是否正在打印。
  • 伙计们。能不能再看一遍。我已经更新了它。我正在尝试从 eventStream 中读取数据,并在请求的负载中使用该数据。
  • 你试过我说的了吗?
  • 好吧。我让它从 eventStream 发送数据,当数据“行”没有出现在 eventstream 中时,脚本停止。 eventStream 并不总是包含来自“line”的数据。这导致我的脚本在没有“line”数据的情况下发送流后崩溃,所以基本上如果流没有“line”数据,它就会崩溃。

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


【解决方案1】:

首先,URL 有问题,所以如果您尝试打印响应文本,将不会输出 那是因为请求失败所以你需要打印状态码才能知道它为什么失败? 不要忘记找到正确的请求正文、标头和 URL,以确保您不会收到错误的请求

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 = 'https://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))
            req = requests.post(url, data=payload, headers=headers, params=querystring)
            print(req.status_code)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2019-04-26
    • 2015-08-01
    • 1970-01-01
    相关资源
    最近更新 更多