Python+redis獲取動態(tài)參數(shù)
1、使用flask編寫接口
-
@server.route()可以將普通函數(shù)轉(zhuǎn)變?yōu)榉?登錄接口的路徑门怪、請求方式
如:@server.route('/in_data', methods=['get'])就是創(chuàng)建一個route名叫in_data的接口晒喷,請求方式為get恩够,以下即完成了Python編寫接口
#!/usr/bin/python3 # -*- coding: utf-8 -*- import redis import flask import json from flask import request from wsgiref.simple_server import make_server ''' flask: web框架,通過flask提供的裝飾器@server.route()將普通函數(shù)轉(zhuǎn)換為服務 ''' # 創(chuàng)建一個服務瓣颅,把當前這個python文件當做一個服務 server = flask.Flask(__name__) @server.route('/in_data', methods=['get']) def in_data(): # 獲取通過url請求傳參的數(shù)據(jù) shop = request.values.get('shop') # 獲取url請求傳的密碼,明文 orders = request.values.get('orders') # mails = request.values.get('mails') keywords = request.values.get('keywords') sell = request.values.get('sell') new = request.values.get('new') amount = request.values.get('amount') account = request.values.get('account') password = request.values.get('password') domain_name = request.values.get('domain_name') # 判斷用戶名唬涧、密碼都不為空 if shop and orders and keywords and sell and new and amount and account and password and domain_name: resu = {'code': 0, 'result': True, 'msg': '操作成功'} connect_redis(shop, orders, keywords, sell, new, amount, account, password, domain_name) return json.dumps(resu, ensure_ascii=False) # 將字典轉(zhuǎn)換字符串 else: resu = {'code': 4001, 'result': False, 'msg': '參數(shù)錯誤'} return json.dumps(resu, ensure_ascii=False) if __name__ == '__main__': server.run(debug=True, port=8080, host='192.x.x.x')
-
Linux中安裝和搭建redis(安裝教程)今艺,前面接口用來獲取參數(shù),下面代碼用來存入redis爵卒,Python調(diào)用redis和存取參數(shù)的代碼如下:
def connect_redis(*args): r = redis.Redis(host='localhost', port=6379, decode_responses=True) shop = args[0] orders = args[1] keywords = args[2] sell = args[3] new = args[4] amount = args[5] account = args[6] password = args[7] domain_name = args[8] r.set("shop", shop) # 店鋪數(shù) r.set("orders", orders) # 單量套餐剩余額度 r.set("keywords", keywords) # 關(guān)鍵詞剩余 r.set("sell", sell) # 跟賣ASIN r.set("new", new) # 上新店鋪 r.set("amount", amount) # 充值金額 r.set("account", account) # 賬號 r.set("password", password) # 密碼 r.set("domain_name", domain_name) # 域名
2、Python/Jmeter請求接口獲取redis數(shù)據(jù)撵彻,傳入動態(tài)參數(shù)
-
Python中使用(可代替Python中的傳遞域名等)
-
Jmeter中使用(可代替Jmeter中使用的CSV)
image