Python如何操作docker redis過程解析
使用操作命令借助subprocess模塊進(jìn)行操作
#encoding:utf-8import subprocessdef cmd(command): subp = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf-8') subp.wait(2) if subp.poll() == 0: return subp.communicate() else: return None獲取redis鍵所對應(yīng)的值def get_output(command): subp = subprocess.getoutput(command) return subp
flask框架獲取docker里面redis中的鍵值對
@ui_case_set.route('/get_code', methods=['GET'])@allow_cross_domaindef get_code(): set_id = request.values.get('id') if not set_id: return response_fail(msg='缺少參數(shù)用例集id') key_name = 'key' + str(set_id) value_name = get_output('docker exec {0} redis-cli get {1}'.format(DockerConfig.container_redis_name, key_name)) if value_name: return response_fail(msg='此測試集正被{}編輯!'.format('金剛')) else: return response_success(msg='可以進(jìn)行編輯!')
flask框架增加及刪除docker里面redis中的鍵值對
@ui_case_set.route('/time_limit', methods=['POST'])@allow_cross_domaindef set_time(): # lock:為1:上鎖, 為0時(shí): 解鎖 set_id = request.json.get('id') locak = request.json.get('lock') # if not all([set_id, locak]): # return response_fail(msg='參數(shù)不足') key_name = 'key' + str(set_id) if locak == 1: value_name = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) cmd('docker exec {0} redis-cli setex {1} 300 ’{2}’'.format(DockerConfig.container_redis_name, key_name, value_name)) return response_success(content={'lock_status': 1}, msg='測試集{}上鎖成功'.format(set_id)) else: cmd('docker exec {0} redis-cli del {1}'.format(DockerConfig.container_redis_name, key_name)) return response_success(content={'lock_status': 0}, msg='測試集{}解鎖成功'.format(set_id))
注意點(diǎn): 使用操作命令時(shí)不要帶 “-it',如(docker exec -it ui_redis(docker容器名稱) redis-cli set key vale) 否則接口在前臺運(yùn)行方式下是可以正常訪問的,在python程序后臺運(yùn)行下運(yùn)行失敗。因?yàn)?指定 -it 是需要開啟一個(gè)交互模式的終端。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea配置jdk的操作方法2. JAMon(Java Application Monitor)備忘記3. SpringBoot+TestNG單元測試的實(shí)現(xiàn)4. Docker容器如何更新打包并上傳到阿里云5. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法6. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題7. VMware中如何安裝Ubuntu8. python 浮點(diǎn)數(shù)四舍五入需要注意的地方9. Springboot 全局日期格式化處理的實(shí)現(xiàn)10. vue實(shí)現(xiàn)web在線聊天功能
