Search code examples
pythonlinuxflaskssh-tunnel

Possible to run flask web app on linux server without web access and access through tunnel?


I work on a linux cluster that is behind a firewall. It does not have web access.

I had this idea I could try to run flask and direct it to a port I know is open (5901 for vnc), and then tunnel that port and view it in my browser.

It's not working so far. Is this possible at all?

Here is what I'm doing:

helloflask.py

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

    if __name__ == "__main__":
        app.run(host='0.0.0.0', port=5901)
        #app.run()

I run python helloflask.py then

ssh -L 5901:<inner server>:5901 <outer server>

Then I navigate to localhost:5901. Nothing. I also tried links localhost:5901 and links <server>:5901, but again nothing.

Is it possible there is some way to do this?


Solution

  • You can do this : Run your flask app or notebook on the remote server on any port. Say for example port 5000.

    On your local machine run below command to establish the ssh tunnelling :

    ssh -D 8123 -f -C -q -N username@remotesrrver
    

    The port 8123 is arbitrary here it can be any allowed port on your local machine. Then setup one of your browser (Fire fox may be) to use socks proxy on port 8123 Make sure that the traffic is proxied for localhost also. By default Firefox disable proxying for localhost. Once these are established you should be able to go to http://localhost:5000 on your browser to hit the app /notebook running on the remote machine

    And your hellowflask.py should be like something below for it to work

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/")
    def hello():
        return "Hello World!"
    
    if __name__ == "__main__":
      app.run(host='0.0.0.0', port=5901)
      #app.run()