Python 實(shí)時(shí)獲取任務(wù)請(qǐng)求對(duì)應(yīng)的Nginx日志的方法
需求描述
項(xiàng)目需求測(cè)試過程中,需要向Nginx服務(wù)器發(fā)送一些用例請(qǐng)求,然后查看對(duì)應(yīng)的Nginx日志,判斷是否存在特征內(nèi)容,來判斷任務(wù)是否執(zhí)行成功。為了提升效率,需要將這一過程實(shí)現(xiàn)自動(dòng)化。
實(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 time
import threading
import subprocess
from collections import deque
def 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日志超時(shí),正在重試')
proc.kill()
try:
outs, errs = proc.communicate(timeout=5)
except subprocess.TimeoutExpired:
print('獲取nginx日志超時(shí),再次超時(shí),停止重試')
break
finally:
for line in outs.split('\n'):
flag = '\"client_ip\":\"10.118.0.77\"' # 特征
if flag in line: # 查找包含特征內(nèi)容的日志
log_for_req += line
if 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ù)請(qǐng)求
# ...
task_status = 'req_finished'
time_to_wait = 0.1
while task_status != 'req_log_got': # 請(qǐng)求觸發(fā)的nginx日志收集未完成
time.sleep(time_to_wait)
time_to_wait += 0.01
else:# 獲取到用例請(qǐng)求觸發(fā)的nginx日志
if nginx_log_queue:
nginx_log = nginx_log_queue.popleft()
task_status = 'req_ready'
# 解析日志
# do something here
# ...
else:
print('存儲(chǔ)請(qǐng)求日志的隊(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í)踐時(shí)發(fā)現(xiàn),第一次執(zhí)行proc.communicate(timeout=2)獲取日志時(shí),總是無法獲取,會(huì)超時(shí),需要二次獲取,并且timeout設(shè)置太小時(shí)(實(shí)踐時(shí)嘗試過設(shè)置為1秒),也會(huì)導(dǎo)致第二次執(zhí)行時(shí)無法獲取Nginx日志。
到此這篇關(guān)于Python 實(shí)時(shí)獲取任務(wù)請(qǐng)求對(duì)應(yīng)的Nginx日志的文章就介紹到這了,更多相關(guān)Python獲取Nginx日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python中四種關(guān)系圖數(shù)據(jù)可視化的效果對(duì)比
python關(guān)系圖的可視化主要就是用來分析一堆數(shù)據(jù)中,每一條數(shù)據(jù)的節(jié)點(diǎn)之間的連接關(guān)系從而更好的分析出人物或其他場(chǎng)景中存在的關(guān)聯(lián)關(guān)系。本文將制作四個(gè)不同的關(guān)系圖的可視化效果,感興趣的可以了解一下2022-11-11
Python定時(shí)庫APScheduler的原理以及用法示例
APScheduler的全稱是Advanced Python Scheduler,它是一個(gè)輕量級(jí)的 Python 定時(shí)任務(wù)調(diào)度框架,下面這篇文章主要給大家介紹了關(guān)于Python定時(shí)庫APScheduler的原理以及用法的相關(guān)資料,需要的朋友可以參考下2021-12-12
python實(shí)現(xiàn)發(fā)送form-data數(shù)據(jù)的方法詳解
這篇文章主要介紹了python實(shí)現(xiàn)發(fā)送form-data數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Python發(fā)送form-data數(shù)據(jù)的相關(guān)操作步驟、實(shí)現(xiàn)方法與注意事項(xiàng),需要的朋友可以參考下2019-09-09
基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能
這篇文章主要介紹了基于python3 pyQt5 QtDesignner實(shí)現(xiàn)窗口化猜數(shù)字游戲功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
解決Django中修改js css文件但瀏覽器無法及時(shí)與之改變的問題
今天小編就為大家分享一篇解決Django中修改js css文件但瀏覽器無法及時(shí)與之改變的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Python實(shí)現(xiàn)自動(dòng)化批量調(diào)整Word樣式
在日常工作中,處理大量的Word文檔是一個(gè)常見的任務(wù),尤其是需要批量修改文檔的樣式時(shí),本文為大家介紹了如何使用Python實(shí)現(xiàn)自動(dòng)化批量調(diào)整Word樣式,需要的可以參考下2024-12-12

