Home > Blockchain >  I can't post to localhost what I see in the terminal
I can't post to localhost what I see in the terminal

Time:08-16

I want to monitor .hprof files in the system using Grafana, so I wrote a small Flask app. I am able to access localhost, I am able to see the number of hprof files on terminal, but I cannot see the number of hprof files on my localhost. Here is my code:

import fnmatch,os
app = Flask(__name__)

@app.route('/')
def hprof():
    num_files = len(fnmatch.filter(os.listdir("/home/ubuntu/files"),'*.hprof'))
    return("Total hprof files: ", num_files)

if __name__ == '__main__':
   app.run(host='0.0.0.0', port=8000, debug=True)
ubuntu@ubuntu-virtual-machine:~/hprof$ python3 hprof.py
 * Serving Flask app "hprof" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:8000/ (Press CTRL C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 284-945-814
127.0.0.1 - - [15/Aug/2022 11:58:05] "GET / HTTP/1.1" 3 -

What I see on localhost:

Total hprof files: 

Can anyone briefly explain how to solve my problem?

CodePudding user response:

I had the same issue. I solved it by entering

localhost:8000

In the browser search bar.

CodePudding user response:

In a view function, returning a tuple like this:

return("Total hprof files: ", num_files)

means that Flask treats it as a (response, status) tuple, so num_files is used for the HTTP status code.

In order to return your intended response, you need to return a single string:

return "Total hprof files: "   str(num_files)

See the relevant Flask docs for more info.

  • Related