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

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

compare.py:

#!/usr/bin/python

import os
import sys
import urllib2
import requests

URI_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 False

if __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://stackoverflow.com/questions/16694907/how-to-download-large-file-in-python-with-requests-py

http://stackoverflow.com/questions/14114729/save-a-large-file-using-the-python-requests-library




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐