python测试http、websocket接口

测试环境有个项目需要每天构造数据,来尽量保证测试环境和生产环境数据量保持一致。需要生成订单后商家接单完成,以下是代码,主要是用接口完成

完成订单代码:

# -*- coding: utf-8 -*

import urllib3
import requests
import logging
import time
import os
from TestAllocation.dcpay.wss import wss
from TestAllocation.dcpay.yaml_ini import read_yaml

urllib3.disable_warnings()

time_log = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime(time.time()))
# log_path = r'E:\PythonWork\StructuralData\TestAllocation\dcpay\logs' + '\\' + time_log + '.log'
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
# logging.basicConfig(filename=log_path, filemode='w+', level=logging.INFO, format='%(asctime)s - %(message)s')

key_dc = read_yaml()['key_dc']
url_login = read_yaml()['url_login']
url_open_take = read_yaml()['url_open_take']
url_rob = read_yaml()['url_rob']
url_pay = read_yaml()['url_pay']
# url_upon = read_yaml()['url_upon']
dc_head = read_yaml()['dc_head']
# backstage_head = read_yaml()['backstage_head']
dc_phone = read_yaml()['dc_phone']


def login():
    list_uid = []
    list_head = []
    for line in dc_phone:
        try:
            # requests.adapters.DEFAULT_RETRIES = 10
            # s = requests.session()
            # s.keep_alive = False

            sessionRequest = requests.session()

            # 登陆
            app_login = app_login 
            r_login = sessionRequest.post(url=url_login, data=data, headers=dc_head, verify=False, timeout=5)
            # print(r_login.url)
            logging.info(u'登陆返回结果:' + r_login.text)

            # 请求头
            uid = r_login.json()['data']['uid']
            u_token = dict(r_login.headers)['utoken']
            t_token = dict(r_login.headers)['ttoken']
            head = head

            list_uid.append(uid)
            list_head.append(head)
            # logging.info(u'HEAD: ' + str(list_head) + '\n')
            # logging.info(u'HEAD: ' + str(list_head) + '\n')

        except BaseException as msg:
            logging.info(u'---ERROR:' + str(msg) + '\n\n\n')

    return list_uid, list_head


def accomplishOrderDc():
    list_data = login()
    list_uid = list_data[0]
    list_head = list_data[1]

    while True:
        for uid, head in zip(list_uid, list_head):
            try:
                # logging.info(uid)
                # logging.info(head)
                sessionRequest = requests.session()

                # # 商家支付信息列表
                # merchant_pay_list = requests.get(url=url_code_list, headers=head, verify=False)
                
                # # 二维码id
                # code_wx_id = merchant_pay_list.json()['data'][0]['id']
                # code_zfb_id = merchant_pay_list.json()['data'][1]['id']
                # r_start_grab = requests.post(url=url_start_grab, data=data},
                #                              headers=head, verify=False)
                # logging.info('设置二维码返回结果:' + r_start_grab.text)

                r_start = sessionRequest.post(url=url_open_take, data=data, headers=head, verify=False,
                                              timeout=5)
                # logging.info('开始接单返回结果:' + r_start.text)

                # # r_buy_off = requests.post(url=url_buy, params=data, headers=head, verify=False)
                # # logging.info('关闭买币接单返回结果:' + r_buy_off.text)
                # #
                # # url_sell_off = requests.post(url=url_buy, params=data,
                # #                              headers=head,
                # #                              verify=False)
                # # logging.info('关闭卖币接单返回结果:' + url_sell_off.text)
                #
                # r_buy = requests.post(url=url_buy, data=data, headers=head, verify=False)
                # logging.info('开启买币接单返回结果:' + r_buy.text)
                #
                # r_sell = requests.post(url=url_buy, data=data, headers=head,
                #                        verify=False)
                # logging.info('开启卖币接单返回结果:' + r_sell.text)

                try:
                    if wss(uid) is not None:
                        # 抢单
                        order_id = wss(uid)
                        r_rob = sessionRequest.post(url=url_rob, data=data, headers=head, verify=False,
                                                    timeout=10)
                        # logging.info(u'抢单结果:' + r_rob.text)

                        amount = r_rob.json()['data']['amount']['reality']
                        order_zid = r_rob.json()['data']['id']

                        # 完成订单
                        r_pay = sessionRequest.post(url=url_pay, data=data, headers=head,
                                                    verify=False,  timeout=5)
                        logging.info(r_pay.text + '   uid' + uid + '   orderId: ' + order_zid + '\n')

                except BaseException as msg:
                    logging.info(u'ERROR:' + str(msg) + '\n')
            except BaseException as msg:
                logging.info(u'ERROR:' + str(msg) + '\n')


if __name__ == '__main__':
    accomplishOrderDc()

接收订单代码:

# coding=utf-8

import re
from websocket import create_connection
from TestAllocation.dcpay.yaml_ini import read_yaml


def wss(uid):
    try:
        ws = create_connection(read_yaml()['url_wss'])

        ws.send('data')
        num = 0
        while True:
            ws.send('data')
            send_data = ws.recv()
            if 'invoice' in str(send_data):
                # print(send_data)
                order_id = re.findall(r'', send_data)
                # print(order_id)

                return order_id

            if num == 100:
                break

            num = num + 1

    except BaseException as msg:
        print('ERROR: ' + msg)


if __name__ == '__main__':
    wss('uid')

你可能感兴趣的:(python)