【问题标题】:cURL works through terminal but not in pythoncURL 通过终端工作,但在 python 中不工作
【发布时间】:2020-04-06 19:55:41
【问题描述】:

我有两台 GoPro 摄像机,我可以通过 10.5.5.9/x/x/x/ 通过 WiFi 控制它们 - 我的想法是我可以使用两个不同的网络接口来控制这两个摄像机,以连接到两台摄像机,然后发送请求到 URL(来自之前的)并控制两个摄像头。

为了测试我的理论,我在终端中执行了以下命令:

curl --interface wlan0 http://10.5.5.9/gp/gpControl/command/shutter?p=1

curl --interface wlan1 http://10.5.5.9/gp/gpControl/command/shutter?p=1

随后,这会激活两个摄像头并开始录制。太好了!

将此代码放入 Python 我尝试了以下简单版本:

import os

resp = os.popen('curl --interface wlan0 http://10.5.5.9/gp/gpControl/command/shutter?p=1').read()
print(resp)
resp = os.popen('curl --interface wlan1 http://10.5.5.9/gp/gpControl/command/shutter?p=1').read()
print(resp)

但它只激活了一个摄像头而没有激活另一个,这背后的原因是什么?我使用this问题的答案做了类似的方法,它也做了同样的事情,只激活了一个摄像头。

【问题讨论】:

  • - 使用python,同一个摄像头只激活一次?还是两次?询问的原因是尝试了解在将命令传递给操作系统时是否正在考虑 --interface
  • - 考虑在 os.popen 上使用子进程(已弃用)docs.python.org/3.8/library/…
  • @silver 它将命令发送到一台摄像机,然后尝试将相同的命令发送到同一台摄像机 - 这告诉我它不太可能从不同的接口发送。我链接到的解决方案也会发生同样的事情。
  • 您可以尝试在不同的 IP 子网中配置其中一个 wlan 和第二个摄像头吗?这将删除一些层来查看。你用的是哪个python版本?
  • @silver 看起来像 Python 3.6.9

标签: python curl


【解决方案1】:

尝试 subprocess.run 而不是 os.popen:

import subprocess

resp = subprocess.run(["curl", "--interface", "wlan0", "http://10.5.5.9/gp/gpControl/command/shutter?p=1"], capture_output=True, shell=True)
print(resp)
resp = subprocess.run(["curl", "--interface", "wlan1", "http://10.5.5.9/gp/gpControl/command/shutter?p=1"], capture_output=True, shell=True)
print(resp)

【讨论】:

  • 检查 cmets - 这是问题发布后建议的第一个更改之一,据报道没有效果。 (另外,OP 的 Python 版本足够老,没有run(),因此有必要显式操作 Popen 对象)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-28
  • 2015-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多