Dominik Lemberger
Dominik Lemberger

Reputation: 2426

python flask website error: the requested URL was not found on the server

I have wrote my first flask program, which logs into my device via telnet and gives me the output of a cat command. But I always get the error:

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

This call of the function GetLogs does not work (doesn't even come into the function - tried it with a print() within the function to see)

@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 />")

The other function Unlock works with basically the same code for app.route(...) but with only 1 parameter

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

Here is the rest of the code, that adds a small HTML UI with 2 textfields and 2 buttons:

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

The URL gets generated right with name = chosenName and path = /var/log/messages* |head -10: http://something.com:5001/GetLogs/chosenName%26path%3D/var/log/messages%2A%20%7Chead%20-10/

Upvotes: 2

Views: 2238

Answers (2)

Vignesh SP
Vignesh SP

Reputation: 446

I think the "/" being provided in the GetLogs are creating issues.

I changed them with "%" before providing the path to the GetLogs function and it worked.

Again replace it with "/" once you fed the path to your function.

@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

Upvotes: 0

Tim Thompson
Tim Thompson

Reputation: 311

Looks like it might be related to how you access the query string. Your path variable should be accessed with 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 />")

Upvotes: 2

Related Questions