cURL通过终端工作,但不在python中工作

时间:2020-04-06 19:55:41

标签: python curl

我有两个可以通过WiFi通过10.5.5.9/x/x/x/进行控制的GoPro摄像机-思想过程是,我可以使用两个不同的网络接口控制两个GoPro摄像机以连接到两个摄像机,然后发送请求转到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问题的答案做了类似的方法,并且仅激活一台摄像机,它的作用相同。

1 个答案:

答案 0 :(得分:0)

尝试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)