【问题标题】:Using a python string output in curl在 curl 中使用 python 字符串输出
【发布时间】:2018-04-06 11:49:31
【问题描述】:

我有这个 python 脚本

users=['mark','john','steve']

text=''

for user in users:
    text+=str(user + " ")

print(text)

我想将该字符串“文本”输出到 curl 终端命令中。

我试过了:

curl -d "@python-scirpt.py" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": 'python /home/scripts/python-script.py'}' https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

或在文本选项中不加引号

一切都返回这个错误

{"id":"Unable to parse incoming data","message":"Unable to parse incoming data","detailed_error":"","request_id":"fpnmfds8zifziyc85oe5eyf3pa","status_code":400}

如何解决这个问题?感谢您提供任何帮助。

另一种方法是在 python 中卷曲,但也需要帮助。 谢谢你

【问题讨论】:

  • Python 有 requests 模块来发出 HTTP 请求。
  • 在 python 脚本中,您可以通过 ' '.join(users) 从列表中创建字符串来代替运行所有操作
  • 谢谢@MartínGómez 我对python很陌生,所以对此一无所知,非常有帮助。
  • @ABDULNIYASPM 您是否愿意指导一下它或引用一个链接或任何东西
  • requests docs 是一个很好的资源

标签: python string curl output


【解决方案1】:

使用命令替换(即$(...))使shell首先运行python代码。

所以

curl -d "$(python-scirpt.py)" --insecure -i -X POST https://10.10.10.6/hooks/84kk9emcdigz8xta1bykiymn5e

【讨论】:

  • /home/scripts/python-script.py:第 12 行:意外标记附近的语法错误`('/home/scripts/python-script.py:第 12 行:`text=''。 join(users)' curl: (51) SSL: certificate subject name 'chat2.temple.lan' does not match target host name '10.10.10.6' 奇怪的是它说 chat2.temple.lan 因为它是 chat.temple.lan
  • 第一个错误与您的 Python 脚本有关。自己运行它,直到你让它工作。第二个错误可能是因为您尝试使用 https 作为 IP 地址,但该站点似乎有一个仅链接到其域名的证书(即 chat2.temple.lan)。如果它在本地受信任的网络上,您可以使用 http。
  • 另外,当我运行它时 curl --insecure -i -X POST -H 'Content-Type: application/json' -d '{"text": "Testing"}' @987654321 @它有效
  • 如果您以 JSON 格式发送数据,您可能应该使用 json.dumps 以确保其格式正确,请参阅 docs.python.org/3/library/json.html
猜你喜欢
  • 2012-02-10
  • 2017-11-30
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多