Click here to Skip to main content
15,867,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi forum,

atm, I'm able to
1) Use Visual Studio Code's debugger on my flask socket.io application and
2) serve clients that are not my development machine but
not both at the same time.

To use the debugger, I just hit "F5" from within VSCode. That runs the application and I can connect to it from the same machine with a browser calling
http://localhost:5000


But calling my application from another machine in our LAN doesn't work
http://192.168.123.10:5000
just ends in a timeout.

But if I start the app from the command-line
winpty python -m app.py --host='0.0.0.0' --port=80
, then I can connect to it from another machine in the LAN. And I cannot user breakpoints, variable inspection and such that way.

I want both. Just hit "F5", use breakpoints and see how application behaves while it serves requests from browsers on other machines.

You will have noticed the strange port specifications, haven't you?

I would like to serve port 80. Therefore in app.py is this:
Python
if __name__ == "__main__":
    #app.debug = False
    socketio.run(app, host='0.0.0.0', port=80)
But this port assignment doesn't seem to work. After all, a local browser reaches the application on port 5000.

How am I supposed to start this application in debugger and connect to it from remote?

What I have tried:

Tried the commented-out app.debug = False, sometimes with = True
Posted
Updated 8-Mar-22 20:21pm

1 solution

Got it.

That __name__ == "__main__" check fails when debugging because Visual Studio Code doesn't start app.py directly but something called launcher instead. VSC also passes launcher some obscure arguments that tell it to start and debug app.py. In this situation, app.py is not the main module and therefore doesn't execute code from that block.

Anyway, the Flask server does start. That's due to some of the obscure arguments, I suppose. And here we can turn the tides in our favour: We can configure that launcher via its launch.json configuration file. I now have this one:
JSON
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "app.py",
                "FLASK_ENV": "development",
                "FLASK_DEBUG": "0"
            },
            "args": [
                "run",
//                "--no-debugger"
                "--host=0.0.0.0",
                "--port=80"
            ],
            "justMyCode": false,
            "jinja": true
        }
    ]
}
The two lines after the single commented-out one are the addition that starts the Flask server the way we want it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900