在使用 Python 编写测试脚本过程中, 进行服务端功能的测试
这个文档主要记录脚本中出现的库的安装方式和一些简单的使用说明
requests
这个库主要是用来进行http请求的发送
pip install requests --user
requests 常用接口
request(method, url, **kwargs)
下面是官方文档中对这个函数的完整描述
"""Constructs and sends a :class:`Request <Request>`.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) <timeouts>` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response <Response>` object
:rtype: requests.Response
接下来简单说一下使用和提几个常用的param
,
methon
中填写请求的类型, 例如GET
,POST
,DELETE
等等url
字段填写http的正常地址- 接下来是可扩充的额外字段
header
中放一些请求的头信息, 比如指明解析方式的Content-Type:application/json
, 连接方式Connection:keep-alive
等一般都是放在header
中Post
请求中Body体的内容用data
或者json
timeout
自定义请求超时时间
示例:
>>> import requests
>>> req = requests.request('GET', 'https://www.baidu,com')
<Response [200]>
其他接口
其他例如post
, get
, put
, delete
等等, 其实本质上都是通过上面的request
接口, 只是将 method
直接默认设置好了
消息体的处理
一般通信之间都会使用json格式, 传输数据, 这里以json为例:
playbackurl_url = 'http://192.168.1.160:10010/api/cms/history/recordurl/1'
res = requests.request('GET',playbackurl_url)
res_json = res.json()
code = res_json['code']
errorstring = res_json['errorstring']
url = res_json['result']['url']
收到返回后, 将结果转json, 再从json中取数据, 这里最后第一步对res进行状态的判定, 这里例如192.168.1.160 服务器没有打开, 其实也就没有必要走 res.json()
opencv
这个库目前在测试工程中使用的是它判断rtsp流地址是否有效,测试播放是否成功的部分功能
pip install opencv-python --user
opencv库的功能特别强大,后面会提供官方文档, 有需要的可以在研究研究
opencv常用接口
VideoCapture
VideoCapture函数一共有以下三种形式
<VideoCapture object> = cv.VideoCapture()
默认构造, 之后通过open()传入和下面2个函数传入参数一致<VideoCapture object> = cv.VideoCapture(filename[, apiPreference])
打开视频文件或捕获设备或IP视频流以进行视频捕获。- 支持传入视频文件名称
- 图像序列(例如 img_00.jpg, img_01.jpg, img_02.jpg, …)
- 视频流的URL
<VideoCapture object> = cv.VideoCapture(index[, apiPreference])
打开摄像头进行视频捕捉index
要打开的视频捕获设备的ID。 要使用默认后端打开默认摄像头,只需传递0.
apiPreference
这个字段不是很理解, 先抄一下官方说明
preferred Capture API backends to use. Can be used to enforce a specific reader implementation if multiple are available: e.g. cv::CAP_DSHOW or cv::CAP_MSMF or cv::CAP_V4L. ( 首选使用的Capture API后端。 如果有多个可用,则可用于强制执行特定的读取器实现:例如 cv :: CAP_DSHOW或cv :: CAP_MSMF或cv :: CAP_V4L。)
代码说明
直接播放RTSP地址的Demo
import cv2
rtsp_url = 'rtsp://admin:hk234567@192.168.10.103:554'
cap = cv2.VideoCapture(rtsp_url)
while True:
ret, frame = cap.read() // 按帧读取视频, ret读取是否成功, frame帧的图像信息(三位矩阵)
cv2.imshow('Live', frame) // 将frame 在一个titlenamne是 `Live` 的窗口下播放
c = cv2.waitKey(1) // waitKey(1) 延时1ms切换下一帧图像, 返回值是键盘输入的ASCII码,esc键对应的是27
if c == 27:
break
cap.release() // 释放播放器资源
cv2.destroyAllWindows() // 关闭所有图像窗口
实际测试RTSP地址Demo
import cv2
rtsp_url = 'rtsp://admin:hk234567@192.168.10.103:554'
cap = cv2.VideoCapture(rtsp_url)
if cap.isOpened():
//播放成功记录
else:
//播放失败信息记录
cap.release() // 释放播放器资源