Python如何操作docker redis過(guò)程解析
使用操作命令借助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鍵所對(duì)應(yīng)的值def get_output(command): subp = subprocess.getoutput(command) return subp
flask框架獲取docker里面redis中的鍵值對(duì)
@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='此測(cè)試集正被{}編輯!'.format('金剛')) else: return response_success(msg='可以進(jìn)行編輯!')
flask框架增加及刪除docker里面redis中的鍵值對(duì)
@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='測(cè)試集{}上鎖成功'.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='測(cè)試集{}解鎖成功'.format(set_id))
注意點(diǎn): 使用操作命令時(shí)不要帶 “-it',如(docker exec -it ui_redis(docker容器名稱(chēng)) redis-cli set key vale) 否則接口在前臺(tái)運(yùn)行方式下是可以正常訪問(wèn)的,在python程序后臺(tái)運(yùn)行下運(yùn)行失敗。因?yàn)?指定 -it 是需要開(kāi)啟一個(gè)交互模式的終端。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 理解PHP5中static和const關(guān)鍵字2. IntelliJ IDEA安裝插件的方法步驟3. php模擬實(shí)現(xiàn)斗地主發(fā)牌4. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟5. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)6. jQuery 實(shí)現(xiàn)DOM元素拖拽交換位置的實(shí)例代碼7. Vuex localStorage的具體使用8. vue 使用localstorage實(shí)現(xiàn)面包屑的操作9. spring acegi security 1.0.0 發(fā)布10. MyBatis中的JdbcType映射使用詳解

網(wǎng)公網(wǎng)安備