?config.yaml配置文件內(nèi)容
功能就是userpass下的用戶名和密碼做增刪改查,并重啟hy2服務
auth:
type: userpass
userpass:
csdn: csdn
listen: :443
masquerade:
proxy:
rewriteHost: true
url: https://www.bing.com/
type: proxy
tls:
cert: /root/hyst*****馬賽克******eria2/csdn.crt
key: /root/hyst*****馬賽克******eria2/csdn.key
直接上代碼
from flask import Flask, request, jsonify
import yaml
import subprocess
import os
app = Flask(__name__)
CONFIG_FILE = '/root/hyst*******馬賽克******eria2/config.yaml'
API_KEY = '123456789'
def read_config():
with open(CONFIG_FILE, 'r') as file:
return yaml.safe_load(file)
def write_config(config):
with open(CONFIG_FILE, 'w') as file:
yaml.safe_dump(config, file)
def restart_service(service_name):
try:
subprocess.run(['sudo', 'systemctl', 'restart', service_name], check=True)
return True
except subprocess.CalledProcessError:
return False
def check_service_status(service_name):
try:
result = subprocess.run(['sudo', 'systemctl', 'is-active', service_name], check=True, stdout=subprocess.PIPE)
if result.stdout.decode('utf-8').strip() == 'active':
return True
else:
return False
except subprocess.CalledProcessError:
return False
@app.route('/api', methods=['POST'])
def manage_user():
# 驗證API Key
api_key = request.headers.get('Authorization')
if api_key != API_KEY:
return jsonify({'error': 'Unauthorized'}), 401
# 解析請求數(shù)據(jù)
data = request.json
if not data or 'username' not in data or 'action' not in data:
return jsonify({'error': 'Bad Request'}), 400
username = data['username']
action = data['action'].lower()
# 讀取配置文件
config = read_config()
userpass = config.get('auth', {}).get('userpass', {})
service_name = 'hyst*******馬賽克******eria2' # 服務名稱
need_restart = False
if action == 'add':
if 'password' not in data:
return jsonify({'error': 'Missing password for add action'}), 400
password = data['password']
userpass[username] = password
need_restart = True
elif action == 'delete':
if username in userpass:
userpass.pop(username, None)
need_restart = True
else:
return jsonify({'error': 'User not found'}), 404
elif action == 'query':
password = userpass.get(username)
if password is not None:
return jsonify({username: password})
else:
return jsonify({'error': 'User not found'}), 404
else:
return jsonify({'error': 'Invalid action'}), 400
# 對于非查詢動作,更新配置文件并重啟服務
if need_restart:
config['auth']['userpass'] = userpass
write_config(config)
if restart_service(service_name):
if check_service_status(service_name):
return jsonify({'success': True, 'message': 'Service restarted and running'})
else:
return jsonify({'error': 'Service restarted but not running'}), 500
else:
return jsonify({'error': 'Failed to restart service'}), 500
return jsonify({'success': True})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
add功能,帶驗證
del功能
?
查詢功能?文章來源:http://www.zghlxwxcb.cn/news/detail-836743.html
代碼完成:chatgpt4?文章來源地址http://www.zghlxwxcb.cn/news/detail-836743.html
到了這里,關于python3 flask 實現(xiàn)對config.yaml文件的內(nèi)容的增刪改查,并重啟服務的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!