python flask网站错误:在服务器上找不到请求的网址

时间:2018-08-01 14:19:12

标签: python 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(...)的代码基本相同,但只有一个参数

@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']))

使用名称= selectedName和路径= / var / log / messages * | head -10:http://something.com:5001/GetLogs/chosenName%26path%3D/var/log/messages%2A%20%7Chead%20-10/

生成URL

2 个答案:

答案 0 :(得分:2)

看起来它可能与您访问查询字符串的方式有关。您的路径变量应使用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 />")

答案 1 :(得分:0)

我认为GetLogs中提供的“ /”正在引起问题。

在提供GetLogs函数的路径之前,我用“%”对其进行了更改。

在将函数的路径输入后,再次用“ /”替换它。

@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']
          abspath=request.form['LogText'].replace("/","%")
          print(abspath)
          return redirect(url_for('GetLogs', name = inputtext, path=abspath))

@app.route('/GetLogs/<name>&<path>/')
def GetLogs(name, path):
  abspath=path.replace("%","")
  return "works"

if __name__ == "__main__":
  app.run(debug=True, host="0.0.0.0", port=80)

After the code change