Last active
August 29, 2015 14:27
-
-
Save macndesign/daf7b9a9e79455ed094a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DBConnection(object): | |
| def __init__(self, database, user, password, host, remote_host, port, db_name): | |
| self.database = database | |
| self.user = user | |
| self.password = password | |
| self.host = host | |
| self.remote_host = remote_host | |
| self.port = port | |
| self.db_name = db_name | |
| def tunnel_conn_str(self): | |
| return 'sshpass -p {password} ssh -N -L {port_1}:{host_1}:{port_2} {user}@{host_2}'.format( | |
| password=self.password, | |
| port_1=self.port, | |
| host_1=self.remote_host, | |
| port_2=self.port, | |
| user=self.user, | |
| host_2=self.remote_host | |
| ) | |
| def oracle_conn_str(self): | |
| return '{user}/{password}@{host}:{port}/{db_name}'.format( | |
| user=self.user, | |
| password=self.password, | |
| host=self.host, | |
| port=self.port, | |
| db_name=self.db_name | |
| ) | |
| def connect(self, q): | |
| process = subprocess.Popen(self.tunnel_conn_str(), shell=True) | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| res, select = None, None | |
| db = Database() | |
| while res != 0: | |
| res = sock.connect_ex((self.host, int(self.port))) | |
| if res == 0: | |
| with db_session: | |
| db.bind(self.database, self.oracle_conn_str()) | |
| select = db.select(q) | |
| print(select) | |
| sock.close() | |
| process.kill() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment