Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import csv
|
| 3 |
+
import requests
|
| 4 |
+
import threading
|
| 5 |
+
from flask import Flask, render_template, request, jsonify, send_file
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
results = []
|
| 10 |
+
lock = threading.Lock()
|
| 11 |
+
|
| 12 |
+
@app.route('/')
|
| 13 |
+
def index():
|
| 14 |
+
return render_template('index.html', results=results)
|
| 15 |
+
|
| 16 |
+
@app.route('/upload', methods=['POST'])
|
| 17 |
+
def upload():
|
| 18 |
+
file = request.files['file']
|
| 19 |
+
if file.filename.endswith('.csv'):
|
| 20 |
+
file.save('/tmp/hosts.csv')
|
| 21 |
+
return '', 204
|
| 22 |
+
return '请上传 CSV 文件', 400
|
| 23 |
+
|
| 24 |
+
@app.route('/start', methods=['POST'])
|
| 25 |
+
def start():
|
| 26 |
+
threading.Thread(target=run_tests).start()
|
| 27 |
+
return jsonify({'status': 'started'})
|
| 28 |
+
|
| 29 |
+
@app.route('/progress')
|
| 30 |
+
def progress():
|
| 31 |
+
with lock:
|
| 32 |
+
return jsonify(results)
|
| 33 |
+
|
| 34 |
+
@app.route('/download')
|
| 35 |
+
def download():
|
| 36 |
+
return send_file('/tmp/result.csv', as_attachment=True)
|
| 37 |
+
|
| 38 |
+
def test_login(host):
|
| 39 |
+
url = f"http://{host}/login"
|
| 40 |
+
payload = "username=admin&password=admin"
|
| 41 |
+
headers = {
|
| 42 |
+
"accept": "application/json",
|
| 43 |
+
"content-type": "application/x-www-form-urlencoded"
|
| 44 |
+
}
|
| 45 |
+
try:
|
| 46 |
+
response = requests.post(url, headers=headers, data=payload, timeout=10)
|
| 47 |
+
status = response.status_code
|
| 48 |
+
success = False
|
| 49 |
+
try:
|
| 50 |
+
response_json = response.json()
|
| 51 |
+
success = response_json.get("success", False)
|
| 52 |
+
except:
|
| 53 |
+
pass
|
| 54 |
+
with lock:
|
| 55 |
+
results.append(f"{host} - {'成功' if success else '失败'} ({status})")
|
| 56 |
+
if success:
|
| 57 |
+
with open('/tmp/result.csv', 'a', newline='', encoding='utf-8') as f:
|
| 58 |
+
writer = csv.writer(f)
|
| 59 |
+
writer.writerow([host])
|
| 60 |
+
except Exception as e:
|
| 61 |
+
with lock:
|
| 62 |
+
results.append(f"{host} - 异常: {str(e)}")
|
| 63 |
+
|
| 64 |
+
def run_tests():
|
| 65 |
+
with lock:
|
| 66 |
+
results.clear()
|
| 67 |
+
if os.path.exists('/tmp/result.csv'):
|
| 68 |
+
os.remove('/tmp/result.csv')
|
| 69 |
+
|
| 70 |
+
with open('/tmp/hosts.csv', 'r', encoding='utf-8') as f:
|
| 71 |
+
reader = csv.reader(f)
|
| 72 |
+
hosts = [row[0] for row in reader]
|
| 73 |
+
|
| 74 |
+
threads = []
|
| 75 |
+
for host in hosts:
|
| 76 |
+
t = threading.Thread(target=test_login, args=(host,))
|
| 77 |
+
threads.append(t)
|
| 78 |
+
t.start()
|
| 79 |
+
for t in threads:
|
| 80 |
+
t.join()
|
| 81 |
+
|
| 82 |
+
if __name__ == '__main__':
|
| 83 |
+
app.run(host='0.0.0.0', port=7860)
|