目录
4、解决python接口测试返回json乱码问题,解决如下:
1、curl接口测试
参考:https://blog.csdn.net/russ44/article/details/53308838
Windows/mac环境上有很多接口测试的工具,如soapui、postman等,但这些工具在linux平台上使用起来相对不那么便捷。有时候当我们要测试一些外部接口时,当本地无权调用测试路径时,需要将测试建立在linux平台,除了封装简单的请求代码进行实现外,可通过curl实现
1、测试get请求
$ curl http://www.linuxidc.com/login.cgi?user=test001&password=123456
$ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi
3、在命令行直接发送JSON结构数据
curl -H 'content-type: application/json' -X POST -d '{"accountType":"4"}' http://192.168.129.xx/AntiRushServer/api/ActivityAntiRush
curl -XPOST -H'Content-Type: application/json' http://0.0.0.0:15663/15663abc/extract_data -d@extrator.json
# extrator.json文件名
响应消息:
{"code":"4000","message":"参数错误:time的值不是UInt"}
2、 Python脚本完成post接口测试脚本
import requests
new_url="http://10.31.143.2:8989/system/systemOrgan/list"
params = {"access_token": "807ad226-cbcc-4620-9544-8f53e1d51405"}
payload = {
"organId":1,
"pageNumber":1,
"pageSize":10,
"sortBy":"lastUpdateTime",
"sort":"DESC"}
results = requests.post(new_url, params=params, json=payload).status_code
print(results)
body中带json数据的:
def auto_post(text):
url = "http://xxx/address"
# url = ''.join([host, endpoint])
body = \
{
"address": text
}
r = requests.post(url=url, data=json.dumps(body))
result = r.json()
# print(result)
requests.post(
url="",
headers="",
cookies="",
data={
},
params={"k1": "v1", "k2": "v2"},
# url中传递的参数,效果如下
# http://www.baidu.com?k1=v1&k2=v2
)
# 我们可以通过data传递请求体,也可以通过json传递请求体
data = {
"username":"admin",
"pwd":"admin"
},
# 则请求体中的数据为username=admin&pwd=admin
# 参数json
json = {
"username":"admin",
"pwd":"admin"
},
# 则请求体中的数据为{"username":"admin","pwd":"admin"}
requests.post():
https://www.cnblogs.com/puresoul/p/7488700.html
3、运行后访问网页,内容中的中文显示乱码
解决方式:
给app配置app.config[‘JSON_AS_ASCII'] = False,即:
if __name__ == "__main__":
app.run(host='127.0.0.1', port='8080')
变为:
if __name__ == "__main__":
app.config['JSON_AS_ASCII'] = False
app.run(host='127.0.0.1', port='8080')
4、解决python接口测试返回json乱码问题,解决如下:
req = requests.post(url="http://192.168.229.128:8080/cms/manage/loginJump.do",
data={"userAccount": "admin", "loginPwd": "123456"})
results = req.json()
print(json.dumps(results, sort_keys=True, indent=2, separators=(',', ': '), ensure_ascii=False))
# indent 是间隔长度,ensure_asccii解决编码问题
5、python接口自动化测试五:乱码、警告、错误处理
https://www.cnblogs.com/zhongyehai/p/9159552.html
6、后台运行python程序并标准输出到文件
现在有test.py
程序要后台部署, 里面有输出内容,想把日志文件输出到test.log
文件
使用命令:
nohup python -u test.py > test.log
# 正常执行的结果将被保存至.log文件;报错信息将打印至屏幕。
nohup python -u test.py > test.log 2>&1 &
# 最后的&表示后台运行
# 2 输出错误信息到提示符窗口
# 1 表示输出信息到提示符窗口, 1前面的&注意添加, 否则还会创建一个名为1的文件
解释:
1、nohup
- nohup指不断地运行,是no hang up的缩写,指不间断,不挂断。运行一个进程的时候,不想让其在你退出账号时关闭,即可用nohup。
- nohup在不规定的情况下,所以输出内容会到nohup.out中
2、2>&1
0
表示stdin标准输入,用户键盘输入的内容1
表示stdout标准输出,输出到显示屏的内容2
表示stderr标准错误,报错内容2>&1
是一个整体,>
左右不能有空格,即将错误内容重定向输入到标准输出中去。- 加
2>&1
中的&
是为了区别文件1和1
(标准输出),假如为2>1
,那么就成了将错误内容输出到文件1中。
查看
tail -f test.log
如果要实时查看日志文件使用命令cat test.log
查看全部输出使用命令
参考:https://blog.csdn.net/weixin_42840933/article/details/85780125