简体   繁体   English

使用 Python 理解 API 并与之交互

[英]Understanding and Interacting with API using Python

I have a basic API installed as my localhost server that does functions such as add camera, star camera, list cameras, snapshot of camera frame, etc.我安装了一个基本的 API 作为我的本地主机服务器,它执行诸如添加相机、星形相机、列出相机、相机帧快照等功能。

My problem is after following the documentation I still can't seem to interact with it well and get the response I need.我的问题是在遵循文档之后,我似乎仍然无法与它很好地交互并获得我需要的响应。 Here is the code I use to log in and get validation token:这是我用来登录并获取验证令牌的代码:

import requests
import urllib.request
import json


base_url = "http://localhostip:8080/api/user/login?"
parameters = {
    "username": username,
    "password": password
}
auth_tok = requests.post(base_url + urllib.parse.urlencode(parameters)).json()
print(auth_tok)

I get the correct documented response with a token, so following the documentation to add camera I need 2 parameters, URL and Name , so I did:我使用令牌获得了正确的记录响应,因此按照文档添加相机我需要 2 个参数, URLName ,所以我做到了:

base_url = "http://localhostip:8080/api/camera/add?"

parameters = {

    "url": 'rtsp://192.168.1.23/1', 

    #or video file 
    "url" : '/home/video/sample.mov'

    "name" : 'cam1'

}
r = requests.post(base_url + urllib.parse.urlencode(parameters),headers={'Authorization': auth_tok})

when I print the response:当我打印响应时:

-print (r)
-print (r.url)
-print(r.status_code)
-print(r.json())

I get this:我明白了:

<Response [500]>

http://192.168.0.162:8080/service/api/camera/add?url=rtsp%3A%2F%2Frtsp%3A%2F%2F192.168.1.23&name=cam1

500

{'code': -111, 'message': None}

According to documentation the correct url should be like this:根据文档,正确的网址应该是这样的:

http://192.168.0.6:8080/service/api/camera/add?url=rtsp://192.168.1.23&name=cam1

and the response should be:响应应该是:

Response: {"status":"ok"}

So why and how to make the URL POST in the correct format, because I suspect this is the issue, the URL has these encoding symbols that may be messing up the request?那么为什么以及如何以正确的格式制作 URL POST,因为我怀疑这是问题所在,URL 具有这些可能会扰乱请求的编码符号?

When I use the web browser GUI of this API I can add the camera or even a video file to play but I'm trying to do the same with Python so I can do further processing in future.当我使用这个 API 的 Web 浏览器 GUI 时,我可以添加相机甚至视频文件来播放,但我正在尝试用 Python 做同样的事情,以便我将来可以做进一步的处理。

Your problem is when you encode the ' / / ' symbol, so, in order to fix that, you need to use another function from urllib, urllib.parse.unquote(), and use as parameter your encoding function urllib.parse.urlencode(parameters):您的问题是当您对“//”符号进行编码时,因此,为了解决该问题,您需要使用 urllib 中的另一个函数 urllib.parse.unquote(),并将您的编码函数 urllib.parse.urlencode 用作参数(参数):

import urllib

parameters = {
    "url": 'rtsp://192.168.1.23/1',
    "name" : 'cam1'
}

The results are :结果是:

print(urllib.parse.urlencode(parameters))
'url=rtsp%3A%2F%2F192.168.1.23%2F1&name=cam1'

print(urllib.parse.unquote(urllib.parse.urlencode(parameters)))
'url=rtsp://192.168.1.23/1&name=cam1'

Source https://docs.python.org/3.0/library/urllib.parse.html#urllib.parse.unquote来源https://docs.python.org/3.0/library/urllib.parse.html#urllib.parse.unquote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM