博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Requests模块
阅读量:1973 次
发布时间:2019-04-27

本文共 4085 字,大约阅读时间需要 13 分钟。

需求一:现在有一个需求就是系统进行了改进(比如将读取配置文件获取数据信息改成了请求gateway来获取数据,这样更加灵活且可改变和配置),线上系统和改进后的测试系统需要测试api来对比获取的数据是否一致从而升级系统,因此可以通过python强大的requests模块来请求线上和测试系统的api来对比结果。

那么先贴一下脚本再简要介绍一下requests的使用:

compare.py:

#!/usr/bin/pythonimport osimport sysimport urllib2import requestsURI_API_ONLINE = "http://10.0.0.1"URI_API_TEST = "http://10.0.0.2"def get_data(api,line):    url = api + '/' + line    res = requests.get(url)    if res.status_code == 200:        return res.json()def compare(d_online, d_test):    if d_online['code'] == 'A00000' and d_test['code'] == 'A00000':        data_online = d_online['data']        data_test = d_test['data']        #online data        internal_uri_online = data_online['internal_uri']        uri_online = data_online['uri']        external_uri_online = data_online['external_uri']        storage_online = data_online['storage']        #test data        internal_uri_test = data_test['internal_uri']        uri_test = data_test['uri']        external_uri_test = data_test['external_uri']        storage_test = data_test['storage']        if internal_uri_test == internal_uri_online and uri_test == uri_online and external_uri_test == external_uri_online and storage_online == storage_test:            return True        else:            return Falseif __name__ == "__main__":    fd = open('./linklist','r')    for line in fd:        line = line.strip()        data_online = get_data(URI_API_ONLINE,line)        data_test = get_data(URI_API_TEST,line)        res = compare(data_online,data_test)        if res:            print 'yes,same.'        else:            print 'no,not same'            print line            print data_online            print data_test            print '\n'

linklist文件:

/uri?bizid=appstore_prod&location=jy&production=appstore&type=prod&role=appstore/uri?bizid=appstore_test&location=jy&production=appstore&type=prod&role=appstore/uri?bizid=bj_ptc_log_prod&location=bj&production=ptc_log&type=prod&role=ptc_log/uri?bizid=bj_qixiao_prod&location=bj&production=qixiao&type=prod&role=qixiao/uri?bizid=cserver_prod&location=jy&production=cserver&type=prod&role=cserver/uri?bizid=cserver_test&location=jy&production=cserver&type=test&role=cserver/uri?bizid=jy_71src_prod&location=jy&production=71src&type=prod&role=71src/uri?bizid=jy_ota_prod&location=jy&production=ota&type=prod&role=ota/uri?bizid=lutai_prod&location=bj&production=lutai&type=prod&role=lutai/uri?bizid=lutai_test&location=bj&production=lutai&type=test&role=lutai/uri?bizid=papaqi_prod&location=jy&production=papaqi&type=prod&role=papaqi/uri?bizid=papaqi_test&location=jy&production=papaqi&type=test&role=papaqi/uri?bizid=reading_prod&location=bj&production=reading&type=prod&role=reading/uri?bizid=reading_test&location=bj&production=reading&type=test&role=reading/uri?bizid=vtc_gif_prod&location=sh&production=vtc_gif&type=prod&role=vtc_gif/uri?bizid=vtc_gif_test&location=sh&production=vtc_gif&type=test&role=vtc_gif

下载文件并保存到本地文件中:

resp = requests.get(url)with open(download_file_path,"wb") as f:	f.write(resp.content)
文件保存到download_file_path中。

Post Json格式表单数据

> curl
-d '{"auth": {"tenantName": "demoTenant", "passwordCredentials":{"username": "admin", "password": "admin"}}}' -H "Content-type: application/json" http://XXXX:35357/v2.0/tokens | python -mjson.tool
>错误:resp = requests.post(url,headers=headers,data=auth)
报错:400 Bad Request
Malformed json in request body
正确1:resp = requests.post(url,headers=headers,data=json.dumps(auth))
正确2:resp = requests.post(url,headers=headers,json=auth)

Requests GET大文件的方法

需要设置requests的get中的stream参数为True。

def download(uri):    global token    headers = {'X-Auth-Token':token}    resp = requests.get(uri,headers=headers,stream=True)    with open(("%s/%s" % (data_dir,"file_download")),"wb") as f:        for chunk in resp.iter_content(chunk_size=1024):            if chunk:                                                                                                                                                                                        f.write(chunk)    return ("%s/%s" % (data_dir,"file_download"))
Reference:

转载地址:http://fvnpf.baihongyu.com/

你可能感兴趣的文章
CDH
查看>>
行为树 BT
查看>>
Cassandra & CQL
查看>>
Oracle数据库
查看>>
Oracle数据库命令
查看>>
有限状态机FSM
查看>>
Win10 Docker
查看>>
Python绘制动画并保存为gif/mp4 (matplotlib)
查看>>
PRM概率路线图
查看>>
ROS(六)——订阅者Subscriber的编程实现(C++ & Python)
查看>>
ROS(七)——话题消息的定义与使用
查看>>
yolov3入门实战
查看>>
B树 & B+树
查看>>
Node-Red(一)——简介与安装
查看>>
representation learning 表示学习/表征学习
查看>>
Haar特征
查看>>
Stereo Matching (双目)立体匹配 & 视差图 & 双目图片进行立体匹配获取深度图进行三维重建的步骤
查看>>
Python 之 histogram直方图
查看>>
Python 之 Scatter散点图
查看>>
Python实现决策树 Desision Tree & 可视化
查看>>