0
votes

I am trying to host a flask server from my windows computer so I can access it from external devices

I am using Flask/Python and have already tried a few things but can't get it to work

Tried running it on 0.0.0.0, port 33, 5000, etc. but I still can't access it this way

from flask import Flask, request, abort

app = Flask(__name__)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=33)

When I then run the file I get:

Running on http://0.0.0.0:33/ (Press CTRL+C to quit)

But it isn't even running there, nor on any other way I can access it

I expect to be able to access my flask application and send requests to it by using my public IP address

What can I do here to make it work?

1
If you have needed ports opened, and via some Server tech like nginx ... but better solution would be to set everything up on an appropriate hostingvi_me
I want to be able to execute commands locally, like open an app or do a web search So that's why I want this to be on my computer instead of a hosting servicePlaceholder 2233
Have you tried using your machines IP address instead of 0.0.0.0? This should allow you to see it locally, and probably across the local network (although I have no other machines on my network here to test that theory). To see it on the outside you'll need some kind of NAT setup in your router. Heed the warnings about not using Flasks inbuilt server in production (especially with debug on), but it should be ok for testing. Also, the first 1024 ports are 'reserved', you should probably use high-number ports (5001 maybe?). Cheers, SteveSteve
I am able to host it over at 0.0.0.0, that's what I tried. Can't really figure out where to look for it after I do app.run() and the app hosts. At which address should I look for it? When I try to test on a device that is not the comptuer that I host it on, I get, the site can't be reached. I am connected to the same networkPlaceholder 2233
yes you can host it at 0.0.0.0 but you possibly can't see it (edit: 0.0.0.0 should work, but try a more specific address as shown here just to be sure), I'm not sure of the implications of using that address. Use the local IP address of the machine you're running Flask on, for example: app.run(debug=True, port=8080, host="192.168.0.12") - I've just set up another laptop here to test that and it works for me here. Browse to 192.168.0.12:8080 from the other machine to see it. Use ipconfig to find your local IP address. HTH, cheers, SteveSteve

1 Answers

0
votes

You have missed an important line in your code:

After the line

app = Flask(__name__)

You have to write the line:

@app.route('/')

We use the route() decorator to tell Flask what URL should trigger our function.

And then define a function that will tell what task to be performed in the web app hosted in the respective address. The function might look something like this:

def hello_world():
return 'Hello, World!'

The complete code then will look like:

from flask import Flask
app = Flask(__name__)

@app.route('/')

def hello_world():
    return 'Hello, World!'
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=33)

Hope this helps.