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

创建订单

# coding=utf-8

import requests
import time
import uuid
import hashlib
import hmac
import urllib3
# from config import read_yaml urllib3.disable_warnings() def shunfeng():
# 时间戳
millis = "%d" % (time.time() * 1000) # 订单号
order_id = ''.join((str(uuid.uuid1()).split('-'))) url = 'https://test'
mid = 'test'
secret = 'test' sec_data = {"data": mid,
"data": 'HmacSHA256',
"data": '',
"data": millis,
"data": order_id,
"orderType": "",
"data": "",
"data": "WechatPay",
"data": "",
"data": "110.110.110.110",
"data": "https://test.com",
"data": "CNY",
"data": order_id} sec_data_str = ''
for k in sorted(sec_data):
sec_data_str += k + '=' + sec_data[k] + '&'
sec_data_str = sec_data_str[:-1]
print('\nsing排序后参数:\n'+sec_data_str) signature_str = hmac.new(secret.encode('utf-8'), sec_data_str.encode('utf-8'), digestmod=hashlib.sha256).digest()
sign = signature_str.hex().upper()
print('\nsign:\n' + sign) body_data = {"data": mid,
"data": 'HmacSHA256',
"data": '',
"data": millis,
"data": order_id,
"data": "",
"data": "",
"data": "WechatPay",
"data": "",
"data": "110.110.110.110",
"data": "https://test.com",
"data": "CNY",
"data": order_id,
"data": sign} body_str = ''
for i in body_data.items():
body_str += i[0] + '=' + i[1] + '&'
body_str = body_str[:-1]
print('\nbody参数:\n' + body_str) head = {"Content-Type": "application/x-www-form-urlencoded"} req = requests.post(url=url, data=body_str, headers=head)
print('\n请求url:\n' + req.url)
print('\n下单返回结果:' + req.text) if __name__ == '__main__':
shunfeng()

接收订单推送

# coding=utf-8

import urllib3
import re
from websocket import create_connection urllib3.disable_warnings() def wss(uid): url = 'wss://test'
ws = create_connection(url) ws.send('{"test":2,"tese":16,"test":1,"body":{"test":"test","test":"test","test":"1.0","test":"1.0"}}') while True: ws.send('{"test":5,"test":16,"test":2,"test":{"userId":' + '"' + uid + '"' + '}}')
send_data = ws.recv()
if 'test' in str(send_data): str1 = str(send_data).split('\\') str2 = str(str1).split(' '' ') str3 = ''.join(str(str2).split('\\')) str4 = re.sub("[\"\'[, ]", "", str3) str5 = re.findall(r'invoice:(.*?)=', str4) order_id = ''.join(str5) + '='
# print(order_id) return order_id if __name__ == '__main__':
wss('uid')

登陆-抢单-完成

import urllib3
import requests
import logging
import time
from web_wss import wss urllib3.disable_warnings() time_log = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime(time.time()))
# logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
logging.basicConfig(filename= 'result.log', filemode= 'w+', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def login():
list_uid = []
list_head = [] phone = phone login_header = {'User-Agent': 'test/1.0.0(build:test;ANDROID 9)',
'bndkey': 'test'} for line in phone:
try:
# requests.adapters.DEFAULT_RETRIES = 10
# s = requests.session()
# s.keep_alive = False sessionRequest = requests.session() # 登陆
app_login = {'account': '86,' + line,
'accountKind': '',
'passwd': 'test'}
r_login = sessionRequest.post(url=url_login, data=app_login, headers=login_header, verify=False, timeout=10)
# print(r_login.url) if '登录成功' in r_login.text:
logging.info(u'登陆成功: ' + line + ' - ' + r_login.json()['data']['uid'])
else:
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 = {'User-Agent': 'test/1.0.0(build:test;ANDROID 7.1.1)',
'bndkey': 'test',
'utoken': u_token,
'ttoken': t_token} r_start = sessionRequest.post(url=url_open_take, data={'main': ''}, headers=head, verify=False, timeout=10)
if 'true' in r_start.text:
logging.info(u'接单开启成功: uid - ' + str(uid) + ' - ' + r_start.text)
else:
logging.info(u'接单开启失败: uid - ' + str(uid) + ' - ' + r_start.text) r_sell = requests.post(url=url_sell, data={'merchantDeposit': '', 'playerDeposit': ''}, headers=head, verify=False, timeout=10)
if 'true' in r_sell.text:
logging.info(u'卖币开启成功: uid - ' + str(uid) + ' - ' + r_sell.text)
else:
logging.info(u'卖币开启失败: uid - ' + str(uid) + ' - ' + r_sell.text) list_uid.append(uid)
list_head.append(head) logging.info(u'uid: ' + str(uid) + ' - utoken: ' + str(u_token) + ' - ttoken: ' + str(t_token) + '\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 accomplishOrder():
list_data = login()
list_uid = list_data[0]
list_head = list_data[1] for uid, head in zip(list_uid, list_head):
try:
sessionRequest = requests.session() # list_order_id = []
# list_amount = [] try:
if wss(uid) is not None:
# 抢单
order_id_sec = wss(uid)
r_rob = sessionRequest.post(url=url_rob, data={'id': order_id_sec}, headers=head, verify=False, timeout=10)
logging.info(u'抢单: ' + r_rob.text) # 查询进行中订单
r_get_order = sessionRequest.get(url=url_run_order, headers=head, verify=False, timeout=10) if r_get_order.json()['data'] is not None:
order_data = r_get_order.json()
for i in range(len(r_get_order.json()['data'])):
order_id = order_data['data'][i]['id']
amount = order_data['data'][i]['amount']['reality'] # list_order_id.append(order_id)
# list_amount.append(amount)
# # logging.info('uid: ' + uid + ' - orderId: ' + order_id + '\n')
# return list_order_id, list_amount # 完成订单
r_pay = sessionRequest.post(url=url_pay, data={'id': order_id, 'amount': amount}, headers=head, verify=False, timeout=10)
if 'true' in r_pay.text:
logging.info(u'完成订单: uid - ' + uid + ' orderId: ' + order_id + '\n')
else:
logging.info(u'订单未完成: uid - ' + uid + r_pay.text)
else:
logging.info(u'暂时没有进行中订单: ' + 'uid: ' + uid) 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__':
while True:
accomplishOrder()

随机推荐

  1. Shell文件权限和脚本执行

    一.预备知识 1.shell的作用   2.常识 (1)Tab键自动补全   使用Terminal时,输入命令的前几个字母,敲tab会自动补全命令或文件名.目录等. 好处:操作速度更快:不容易出错: ...

  2. Unity3D GUI学习之GUI窗口的使用

    GUI还可以定义一个窗口,在窗口里面进行控件的添加: using UnityEngine; using System.Collections; public class getbutton : Mon ...

  3. Windows系统基本概念

    windows API:被文档化的可以调用的子例程,如CreateProcess 原生的系统服务(执行体系统服务):未被文档化的.可以再用户模式下调用的底层服务,如NtCreateProcess 内核 ...

  4. 改动mac环境变量,并配置gradle

    由于项目中要用到gradle命令,可是没有配置环境变量.这里记录一下解决过程. 过程例如以下: 1. 启动终端Terminal 2. 进入当前用户的home文件夹 输入cd ~ 3. 创建.bash_ ...

  5. HTTP基本知识

    1.TCP/IP 传输控制协议/因特网互联协议 (1)应用层:决定向用户提供应用服务时通信的活动(FTP.DNS和HTTP都属于该层). (2)传输层:提供处于网络连接中的两台计算机之间的数据传输(T ...

  6. parseInt ,parseDouble,parseFloat

    转载https://www.cnblogs.com/originate918/p/6377616.html

  7. 光速搭lvs + keepalived + nginx

    环境:   VIP         192.168.2.224 LVS        192.168.2.217     centos7 nginx1    192.168.2.231     cen ...

  8. php 图像裁剪(自定义裁剪图片大小)

    <?php /** * 图像裁剪 * @param $title string 原图路径 * @param $content string 需要裁剪的宽 * @param $encode str ...

  9. Android 与 js 简单互调

    总结: Android 调用  js: 在 Android 中创建通往 javascript 的接口; 在 html  中定义要执行的方法; 在  Android  中的具体事件中进行调用. cont ...

  10. haproxy 日志配置

    haproxy日志配置 haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haprox ...