国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Python 實(shí)時獲取任務(wù)請求對應(yīng)的Nginx日志的方法

瀏覽:93日期:2022-06-15 10:13:01
需求描述

項(xiàng)目需求測試過程中,需要向Nginx服務(wù)器發(fā)送一些用例請求,然后查看對應(yīng)的Nginx日志,判斷是否存在特征內(nèi)容,來判斷任務(wù)是否執(zhí)行成功。為了提升效率,需要將這一過程實(shí)現(xiàn)自動化。

實(shí)踐環(huán)境

Python 3.6.5

代碼設(shè)計(jì)與實(shí)現(xiàn)

#!/usr/bin/env python# -*- coding:utf-8 -*-’’’@CreateTime: 2021/06/26 9:05@Author : shouke’’’import timeimport threadingimport subprocessfrom collections import dequedef collect_nginx_log(): global nginx_log_queue global is_tasks_compete global task_status args = ’tail -0f /usr/local/openresty/nginx/logs/access.log’ while task_status != ’req_log_got’:with subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines = True) as proc: log_for_req = ’’ outs, errs = ’’, ’’ try:outs, errs = proc.communicate(timeout=2) except subprocess.TimeoutExpired:print(’獲取nginx日志超時,正在重試’)proc.kill()try: outs, errs = proc.communicate(timeout=5)except subprocess.TimeoutExpired: print(’獲取nginx日志超時,再次超時,停止重試’) break finally:for line in outs.split(’n’): flag = ’'client_ip':'10.118.0.77'’ # 特征 if flag in line: # 查找包含特征內(nèi)容的日志log_for_req += lineif task_status == ’req_finished’: nginx_log_queue.append(log_for_req) task_status = ’req_log_got’def run_tasks(task_list): ’’’ 運(yùn)行任務(wù) :param task_list 任務(wù)列表 ’’’ global nginx_log_queue global is_tasks_compete global task_status for task in task_list:thread = threading.Thread(target=collect_nginx_log, name='collect_nginx_log')thread.start()time.sleep(1) # 執(zhí)行任務(wù)前,讓收集日志線程先做好準(zhǔn)備print(’正在執(zhí)行任務(wù):%s’ % task.get(’name’))# 執(zhí)行Nginx任務(wù)請求# ...task_status = ’req_finished’time_to_wait = 0.1while task_status != ’req_log_got’: # 請求觸發(fā)的nginx日志收集未完成 time.sleep(time_to_wait) time_to_wait += 0.01else:# 獲取到用例請求觸發(fā)的nginx日志 if nginx_log_queue:nginx_log = nginx_log_queue.popleft()task_status = ’req_ready’# 解析日志# do something here# ... else:print(’存儲請求日志的隊(duì)列為空’)# do something here# ...if __name__ == ’__main__’: nginx_log_queue = deque() is_tasks_compete = False # 所有任務(wù)是否執(zhí)行完成 task_status = ’req_ready’ # req_ready,req_finished,req_log_got # 存放執(zhí)行次任務(wù)任務(wù)的一些狀態(tài) print(’###########################任務(wù)開始###########################’) tast_list = [{’name’:’test_task’, ’other’:’...’}] run_tasks(tast_list) is_tasks_compete = True current_active_thread_num = len(threading.enumerate()) while current_active_thread_num != 1:time.sleep(2)current_active_thread_num = len(threading.enumerate()) print(’###########################任務(wù)完成###########################’)

注意:

1、上述代碼為啥不一步到位,直接 tail -0f /usr/local/openresty/nginx/logs/access.log | grep '特征內(nèi)容'呢?這是因?yàn)檫@樣做無法獲取到Nginx的日志

2、實(shí)踐時發(fā)現(xiàn),第一次執(zhí)行proc.communicate(timeout=2)獲取日志時,總是無法獲取,會超時,需要二次獲取,并且timeout設(shè)置太小時(實(shí)踐時嘗試過設(shè)置為1秒),也會導(dǎo)致第二次執(zhí)行時無法獲取Nginx日志。

到此這篇關(guān)于Python 實(shí)時獲取任務(wù)請求對應(yīng)的Nginx日志的文章就介紹到這了,更多相關(guān)Python獲取Nginx日志內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 日韩中文字幕在线免费观看 | 亚洲精品h| 极品美女写真菠萝蜜视频 | 国产91精品一区二区视色 | 91亚洲精品久久91综合 | 加勒比色久综合在线 | 纯欧美一级毛片免费 | 亚洲成在人线免费视频 | 亚洲视频在线观看一区 | 日韩 国产 在线 | 一级做a爰全过程免费视频毛片 | 国产一级毛片一区二区三区 | 欧美一区二区三区免费高 | 欧美精品久久久亚洲 | 色婷婷色综合激情国产日韩 | 一级看片免费视频 | 亚洲经典在线观看 | 成人做爰视频www视频 | 国产精品亚洲欧美日韩久久 | 看日本真人一一级特黄毛片 | freexxxx性特大另类ww | 王朝影院一区二区三区入口 | 超清波多野结衣精品一区 | 免费99视频 | 亚洲免费在线视频播放 | 美女一级ba大片免色野外 | 在线永久免费观看黄网站 | 亚洲国产日韩a在线亚洲 | 国产成人三级经典中文 | www.久久久| 免费观看a视频 | 久色网址| ccav在线永久免费看 | 真人一级毛片免费观看视频 | 怡红院免费va男人的天堂 | 美女张开腿让男人桶的 视频 | 欧美精品网站 | 欧美a大片欧美片 | 日本护士视频xxxxxwww | 颜值超高的女神啪啪 | 波多野结衣一级片 |