我有PTZ摄像头,我尝试通过CURL以不同的方式访问该摄像机。我打算通过网络界面访问摄像机中的预设位置。 基于浏览器调试器访问PTZ摄像机预置的逻辑如下:
以下是使用shell脚本的源代码:
echo "Set PTZ"
echo $1 #IP address
echo $2 #preset
url_login='http://'$1'/login/login/'
url_preset='http://'$1'/ptz/presets.html'
curl -c cookies.txt -s -X POST $url_login --data "user=admin&pass=admin&forceLogin=on"
curl -b cookies.txt -s -X POST $url_preset --data 'PTZInterface!!ptzPositions='$2
curl -b cookies.txt -s -X PUT $url_preset --data 'autobutton=GotoCurrVirtualPreset&object=PTZInterface&id='
我成功使用shell脚本,访问摄像头并进入预设。
但我的主要目的是使用python创建一个程序。以下是我使用请求的python:
import requests
URL_LOGIN = "/login/login/"
PARAMS_LOGIN = {"user": "admin", "pass": "admin", "forceLogin": "on"}
URL_PRESET = "/ptz/presets.html"
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache'}
def set_ptz(arg_camera = None, arg_preset = None):
url_login = "http://" + arg_camera + URL_LOGIN
url_preset = "http://" + arg_camera + URL_PRESET
HEADERS['Host'] = arg_camera
HEADERS['Referer'] = 'http://' + arg_camera + '/'
params = {}
params["PTZInterface!!ptzPositions"] = arg_preset
params_put = {}
params_put["autobutton"] = "GotoCurrVirtualPreset"
params_put["object"] = "PTZInterface"
params_put["id"] = ""
s = requests.Session()
r1 = s.post(url_login, data = PARAMS_LOGIN) # Login -> success
var_cookies = r1.cookies
r2 = s.post(url_preset, cookies = var_cookies, headers = HEADERS, data = params) # Post preset position -> failed
r3 = s.put(url_preset, cookies = var_cookies, headers = HEADERS, data = params_put) # Put execution -> success
print r1.headers
print var_cookies
print r2.headers
print r3.headers
print r3.text
print r1.status_code
print r2.status_code
print r3.status_code
set_ptz('10.26.1.3.61', 1)
我使用PUT成功登录并提交,但未能发布预设位置。我的python代码有什么问题?我认为结果应该是一样的。
感谢您的帮助。
答案 0 :(得分:1)
requests
正在逃避POST数据中的惊叹号:
In [1]: import requests
In [2]: requests.post(..., data={"PTZInterface!!ptzPositions": '1'}).request.body
Out[2]: 'PTZInterface%21%21ptzPositions=1'
cURL只是按原样发送它们。您可以直接将data
作为字符串传递:
In [3]: requests.post(..., data="PTZInterface!!ptzPositions=1").request.body
Out[3]: 'PTZInterface!!ptzPositions=1'
或使用urllib.parse.urlencode
的{{1}}参数构建它:
safe