gpt4 book ai didi

python Flask网站错误: the requested URL was not found on the server

转载 作者:行者123 更新时间:2023-12-01 01:43:26 26 4
gpt4 key购买 nike

我已经编写了我的第一个 Flask 程序,它通过 telnet 登录到我的设备并为我提供 cat 命令的输出。但我总是收到错误:

Not Found
The requested URL was not found on the server.
If you entered the URL manually please check your spelling and try again.

函数 GetLogs 的调用不起作用(甚至没有进入该函数 - 在函数内使用 print() 进行尝试以查看)

@app.route('/GetLogs/<name>&path=<path>/')
def GetLogs(name, path):
tempStr=''
HOST = name
user = "root"
password = "password"
tn = telnetlib.Telnet(HOST, timeout=5)
tn.read_until(b"login: ")
time.sleep(1)
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b'Password: ')
time.sleep(1)
tn.write(password.encode('ascii') + b"\n")
time.sleep(2)
PATH_TO_LOG = "cat " + path + "\n"
tn.write(PATH_TO_LOG.encode('ascii') + b"\n")
tn.write(b"exit\n")
tempStr=tn.read_all().decode('ascii')
return tempStr.replace("\r\n", "<br />")

另一个函数 Unlock 与 app.route(...) 的代码基本相同,但只有 1 个参数

@app.route('/Unlock/<name>/')
def Unlock(name):
return "unlocked"

这是代码的其余部分,添加了一个带有 2 个文本字段和 2 个按钮的小型 HTML UI:

@app.route('/')
def main_form():
return '''<!DOCTYPE html>
<html lang="en">
<body>
<form action="/" method="POST">
<input type="text" name="text">
<br><br><br>
<input type="submit" name="Buttons" value="Unlock">
<br><br>
<input type="submit" name="Buttons" value="GetLogs"><input type="text" name="LogText">
<br><br>
</form>
</body>
</html>
'''


@app.route('/', methods=['POST', 'GET'])
def ExecuteButtons():
if request.method == 'POST':
inputtext = request.form['text']
inputtext = inputtext.replace("http://", "")
inputtext = inputtext.replace("/","")
inputtext = inputtext.replace(".com","")
if request.form['Buttons'] == 'Unlock':
#inputtext = request.form['text']
return redirect(url_for('Unlock', name = inputtext))
elif request.form['Buttons'] == 'GetLogs':
#inputtext = request.form['text']
return redirect(url_for('GetLogs', name = inputtext, path=request.form['LogText']))

生成的 URL 正确,名称 = selectedName 且路径 =/var/log/messages* |head -10: http://something.com:5001/GetLogs/chosenName%26path%3D/var/日志/消息%2A%20%7Chead%20-10/

最佳答案

看起来这可能与您访问查询字符串的方式有关。应使用 request.args.get() 访问您的路径变量。

@app.route('/GetLogs/<name>')
def GetLogs(name):
path = request.args.get("path")
tempStr=''
HOST = name
user = "root"
password = "password"
tn = telnetlib.Telnet(HOST, timeout=5)
tn.read_until(b"login: ")
time.sleep(1)
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b'Password: ')
time.sleep(1)
tn.write(password.encode('ascii') + b"\n")
time.sleep(2)
PATH_TO_LOG = "cat " + path + "\n"
tn.write(PATH_TO_LOG.encode('ascii') + b"\n")
tn.write(b"exit\n")
tempStr=tn.read_all().decode('ascii')
return tempStr.replace("\r\n", "<br />")

关于python Flask网站错误: the requested URL was not found on the server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51635676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com