如何将数据从文本文件传输到HTML5/JavaScript到flask服务器?
警告:这是一个很难理解的复杂问题。我为意大利面代码道歉,但它在这里 我的目标是创建一个discord.py bot,将新的聊天信息记录到文本文件中,然后将该信息发送到我创建的flask服务器。我的问题是将文本信息从文本文件发送到html文件。我的文本文件如下所示(路径:logs/chat.txt): 解析后的数据被发送到html文件'index.html'(路径:templates/index.html)(这是令人困惑的部分): 和服务器:如何将数据从文本文件传输到HTML5/JavaScript到flask服务器?,javascript,file,flask,Javascript,File,Flask,警告:这是一个很难理解的复杂问题。我为意大利面代码道歉,但它在这里 我的目标是创建一个discord.py bot,将新的聊天信息记录到文本文件中,然后将该信息发送到我创建的flask服务器。我的问题是将文本信息从文本文件发送到html文件。我的文本文件如下所示(路径:logs/chat.txt): 解析后的数据被发送到html文件'index.html'(路径:templates/index.html)(这是令人困惑的部分): 和服务器: from flask import Flask, re
from flask import Flask, render_template
from parseLogs import Parser
from threading import Thread
app = Flask('Cheesebot\'s webserver hosting')
'''
@app.route('/')
def index(user, dat, message):
return render_template('index.html', user=user, dat=dat, message=message)
'''
parser = Parser('logs/chat.txt')
msg = parser.getLastMsg()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<msg>')
def imdex(msg):
return render_template('index.html', msg=msg)
def run():
app.run(host='0.0.0.0', port = 8080)
# cmd_inc = ''
# @app.logger()
# def get_new_cmd(inc_cmd, usr):
# return('\n/* new command from user ')
def keep_alive():
t = Thread(target=run)
t.start()
很抱歉造成混淆,但如果您能帮助我解决此问题或找到更好的解决方案,那就太好了。在您的服务器上找不到路径/logs/chat.txt(因此为404)。它必须位于其他文件夹中,或者http请求可能无法访问。
class Parser:
def __init__(self,path):
self.path = path
def getLastMsg(self):
log = open(self.path, 'r')
final = log.read().strip().split('_')
log.close()
print(final[-1])
return(final[-1])
<html>
<head>
<link rel="stylesheet" type="text/css" href="/static/index.css" />
<!--
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1> CheeseBot Control Center</h1>
<p> </p>
<p> </p>
<div id="console" style="background-color:black;color:green;overflow:auto;width:500px;height:700px;">
</div>
<script>
var url = "logs/chat.txt";
var textarea_id = "#incTxt";
setInterval(function(){
$.ajax({
url : "https://replit.com/@David0Weir/DiscB#templates/src.php",
type : "POST",
success : function(data){
$(".textarea").html(data);
}
});
}, 1000);
$(document).ready(function(){
setInterval(function(){
$("#console").load('logs/chat.txt');
}, 1000);
});
</script>
<script>
setInterval(function(){
lastMsg = ''
var nxtMsg = {{ msg }}
if(nxtMsg != lastMsg){
document.getElementById('incTxt').append(nxtMsg + " ")
}
nxtMsg = lastMsg
}, 2000)
</script>
<p><label for="cheesebellies"></label> <textarea style="background: #302e2e; color: white;" id="incTxt" cols="100" name="incTxt" rows="18"> </textarea></p>
<script>
function rlIncTxt() {
document.getElementById("incTxt") + " Message from {{ user }} at {{ dat }}: {{ message }} \n";
console.log("reload incoming text box button is registering js function!");
window.alert("rlIncTxt function is working !")
}
</script>
<p> </p>
<button onclick='rlIncTxt()' style="background-color: #302e2e; border: black; color: white; padding: 5px 16px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer;"> Textarea rlIncTxt </button>
</body>
</html>
$file_path = "logs/chat.txt";
$file_content = file_get_contents($file_path);
echo $file_content;
from flask import Flask, render_template
from parseLogs import Parser
from threading import Thread
app = Flask('Cheesebot\'s webserver hosting')
'''
@app.route('/')
def index(user, dat, message):
return render_template('index.html', user=user, dat=dat, message=message)
'''
parser = Parser('logs/chat.txt')
msg = parser.getLastMsg()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/<msg>')
def imdex(msg):
return render_template('index.html', msg=msg)
def run():
app.run(host='0.0.0.0', port = 8080)
# cmd_inc = ''
# @app.logger()
# def get_new_cmd(inc_cmd, usr):
# return('\n/* new command from user ')
def keep_alive():
t = Thread(target=run)
t.start()
172.18.0.1 - - [10/May/2021 13:35:43] "GET /logs/chat.txt HTTP/1.1" 404 -
172.18.0.1 - - [10/May/2021 13:35:44] "GET /logs/chat.txt HTTP/1.1" 404 -
172.18.0.1 - - [10/May/2021 13:35:44] "GET /logs/chat.txt HTTP/1.1" 404 -
172.18.0.1 - - [10/May/2021 13:35:44] "GET /logs/chat.txt HTTP/1.1" 404 -