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時: 解鎖 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): 使用操作命令時不要帶 “-it',如(docker exec -it ui_redis(docker容器名稱) redis-cli set key vale) 否則接口在前臺運(yùn)行方式下是可以正常訪問的,在python程序后臺運(yùn)行下運(yùn)行失敗。因?yàn)?指定 -it 是需要開啟一個交互模式的終端。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. python中PyQuery庫用法分享3. python操作數(shù)據(jù)庫獲取結(jié)果之fetchone和fetchall的區(qū)別說明4. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)5. CSS3實(shí)現(xiàn)動態(tài)翻牌效果 仿百度貼吧3D翻牌一次動畫特效6. PHP獲取時間戳等相關(guān)函數(shù)匯總7. python 爬取嗶哩嗶哩up主信息和投稿視頻8. 關(guān)于HTML5的img標(biāo)簽9. ASP.NET MVC前臺動態(tài)添加文本框并在后臺使用FormCollection接收值10. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能
