Online Traffic Replay in Python Automated Testing: Log Parsing, MySQL Storage, and Local Request Data Management
This article explains how to implement online traffic replay for Python automated testing by parsing logs, storing request data in a local MySQL database, analyzing user information with data‑analysis libraries, and locally persisting URL, method, headers, and body details using JSON files, complete with sample code.
In automated testing, online traffic replay is a key technique that simulates real user requests to reproduce production scenarios and verify system performance and stability.
The article first describes how to split and parse logs using Python libraries, then store the extracted request records into a local MySQL database, providing a complete code example.
import pymysql
import json
# 连接到本地MySQL数据库
conn = pymysql.connect(host='localhost', user='username', password='password', db='database_name')
cursor = conn.cursor()
# 读取分割解析后的请求记录文件
parsed_log_file = "parsed_log.txt"
with open(parsed_log_file, "r") as file:
content = file.read()
log_data = json.loads(content)
# 存储请求记录到数据库
for request in log_data:
url = request["url"]
method = request["method"]
headers = request["headers"]
body = request["body"]
# 执行SQL语句,将请求记录插入到数据库中的表格
sql = "INSERT INTO request_logs (url, method, headers, body) VALUES (%s, %s, %s, %s)"
values = (url, method, headers, body)
cursor.execute(sql, values)
# 提交事务并关闭数据库连接
conn.commit()
cursor.close()
conn.close()Next, it explains how to analyze online user information such as request counts, paths, and methods by using Python data‑analysis libraries like Pandas and NumPy, enabling the generation of reports and key metrics.
Finally, the article shows how to locally persist URL, method, headers, and body information by writing them to JSON files, with a second code example demonstrating saving and loading request data.
import json
# 请求信息
request_info = {
"url": "https://example.com/api",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer your_token"
},
"body": {
"key1": "value1",
"key2": "value2"
}
}
# 将请求信息保存到本地文件
request_file = "request_info.json"
with open(request_file, "w") as file:
json.dump(request_info, file)
# 从本地文件中读取请求信息
with open(request_file, "r") as file:
content = file.read()
stored_request_info = json.loads(content)
print(stored_request_info)In summary, Python‑based online traffic replay combines log parsing, MySQL storage, and local JSON storage to create a flexible testing workflow that can also provide valuable user behavior insights.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.