Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import hashlibimport timefrom flask import Flask, render_template, request, jsonifyapp = Flask(__name__)current_text = ""logs = []def get_ip_hash(ip):return hashlib.sha256(ip.encode()).hexdigest()[:8]def log_text(ip, text):timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())ip_hash = get_ip_hash(ip)logs.append([timestamp, ip_hash, text])@app.route('/')def index():return render_template('index.html')@app.route('/ViewPort')def viewport():return render_template('viewport.html')@app.route('/Ctrl', methods=['GET', 'POST'])def ctrl():global current_textif request.method == 'POST':new_text = request.form['text']if new_text != current_text: # Check for duplicate contentcurrent_text = new_textlog_text(request.remote_addr, current_text)return '', 204return render_template('ctrl.html')@app.route('/get_text')def get_text():global current_textreturn jsonify({'text': current_text})@app.route('/log')def log():return render_template('log.html', log_data=logs)if __name__ == '__main__':app.run(debug=True, host="0.0.0.0", port=7860)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455from flask import Flask, request, jsonify, render_template_stringfrom datetime import datetimeapp = Flask(__name__)# テキストを記録するためのリストtexts = []@app.route('/post', methods=['POST'])def post_text():# POSTリクエストからテキストを取得text = request.form.get('text')if text:# 現在の日時を取得timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')# テキストとタイムスタンプをリストに追加texts.append({'text': text, 'timestamp': timestamp})return jsonify({'message': 'Text received and recorded', 'text': text, 'timestamp': timestamp}), 201else:return jsonify({'message': 'No text provided'}), 400@app.route('/getText/latest', methods=['GET'])def get_latest_text():if texts:# リストの最後の要素(最新のテキスト)を取得latest_text = texts[-1]return jsonify(latest_text), 200else:return jsonify({'message': 'No texts recorded yet'}), 200@app.route('/')def display_texts():# 記録されたテキストをHTMLで表示html_content = '''<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Recorded Texts</title></head><body><h1>Recorded Texts</h1><ul>{% for entry in texts %}<li><strong>{{ entry.timestamp }}:</strong> {{ entry.text }}</li>{% endfor %}</ul></body></html>'''return render_template_string(html_content, texts=texts)if __name__ == '__main__':app.run(debug=True, host='0.0.0.0', port=7860)
app.py+48-39