How to SSH tunnel into a remote server using python
Context:
- You need to SSH tunnel into a remote server to run your python code.
- Use case, would be a database located in a remote server. For example: db_server_dev and db_server_prod
- Since you can run the code both on your local machine and remote machine, you want to your code to automatically understand which environment it is in and connect to the appropriate server.
Steps:
- Set the correct environment varible for your operating system
- Use SSHTunnel package in python
- Implement attached code customized to your needs
Set ENV variable:
- pycharm: run > edit configurations > environment variables
- linux: echo "export ENV='production'".
Note: Remember to store the creds in a separate file, and dont upload to github!
Reference: https://peps.python.org/pep-0343/