How to expose a Flask local development server to the public using SSH remote port forwarding
Here is how to run a Flask local development server on your local machine and expose it to the public via a remote server you have control over. This uses SSH remote port forwarding which is a converse of local port forwarding described here: How to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding
- On the remote host, edit the sshd_config file (mine was located at /etc/ssh/sshd_config) to allow remote hosts to connect to ports forwarded for the client:
GatewayPorts yes
- On the remote host, restart the SSH server:
$ sudo service sshd restart
- On the local host, SSH to the remote host:
$ ssh -v -R 50051:localhost:5000 [email protected]
- On the local host, run the Flask dev server:
$ python runserver.py localhost 5000
- Go to http://my.remotehost.com:50051 in the browser
Using RemoteForward in your ~/.ssh/config
You can also achieve the same results by using the RemoteForward
in your ~/.ssh/config
file:
Host myremote User eliot HostName my.remotehost.com RemoteForward 50051 localhost:5000
References
- http://www.hackinglinuxexposed.com/articles/20030309.html
- http://serverfault.com/questions/285616/how-to-allow-remote-connections-from-non-localhost-clients-with-ssh-remote-port
- http://linux.die.net/man/5/sshd_config
See also
localtunnel by Jeff Lindsay exposes your local development server without requiring a public remote server.
Related posts
- Notes on Fabric 2 and Python 3 — posted 2021-02-07
- How to run a Django local development server on a remote machine and access it in your browser on your local machine using SSH port forwarding — posted 2012-10-23
- Notes on debugging ssh connection problems — posted 2011-08-31
- Fabric post-run processing Python decorator — posted 2010-11-06
- Class-based Fabric scripts via a Python metaprogramming hack — posted 2010-09-23
Comments
There are a bunch of good services that will do this more easily as well: http://john-sheehan.com/blog/a-survey-of-the-localhost-proxying-landscape
John, thanks for the great list. I'll remember that for the future. At the time I needed this, I actually was trying to expose my dev server at a specific URL that a third server was pointing at. I know my post fails to mention that.
Really helpful. Thanks.
disqus:1931089722