Last active
February 11, 2021 23:18
-
-
Save lbunzl-r7/d8aba7ad11eed0e278cfa5c4f9533dd4 to your computer and use it in GitHub Desktop.
[Python] db connection
This file contains 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
sudo yum -y update | |
sudo yum -y install yum-utils | |
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm --skip-broken | |
sudo yum -y install python36u | |
sudo yum -y install python36u-pip | |
cd /usr/bin | |
sudo ln -s /usr/bin/python3.6 /usr/bin/python3 | |
sudo ln -s -f /usr/bin/python3 /usr/bin/python | |
pip3 install --user robotframework | |
pip3 install --user pexpect |
This file contains 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
sudo echo '#!'`which python` | sudo tee /usr/bin/pybot> /dev/null; | |
sudo echo import sys | sudo tee -a /usr/bin/pybot>/dev/null; | |
sudo echo from robot import run_cli | sudo tee -a /usr/bin/pybot> /dev/null; | |
sudo echo "run_cli(sys.argv[1:])" | sudo tee -a /usr/bin/pybot> /dev/null; | |
sudo chmod 755 /usr/bin/pybot; |
This file contains 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
#Fix Python3 link | |
sudo ln -s -f /usr/bin/python3 /usr/bin/python | |
#Install PIP (V3) | |
sudo curl https://bootstrap.pypa.io/3.3/get-pip.py -o get-pip.py ; sudo python get-pip.py |
This file contains 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
import logging | |
import re | |
logging.info = print | |
try: | |
import psycopg2 # pip install psycopg2-binary | |
except ImportError: | |
print("Need psycopg2 DB interface in env") | |
def verify_a_connection(conn_str): | |
logging.info("Trying to connect...") | |
try: | |
conn = psycopg2.connect(conn_str) | |
logging.info("Connected.") | |
return (0) | |
except (Exception, psycopg2.DatabaseError) as error: | |
logging.info("PG Error Code: " + str(error)) | |
logging.info("Unable to connect to the database.") | |
return (1) | |
def get_conn_string_db(user, host, port, dbname, pswd): | |
conn_str = "host='" + host + "' port='" + str( | |
port) + "' dbname='" + dbname + "' user='" + user + "' password='" + pswd + "'" | |
logging.info("get_conn_string_db:(USER) ....") | |
logging.info("Connection String: " + re.sub("password\=.*$", "password='*****'", conn_str)) | |
return (conn_str) | |
user = "" # add username here | |
host = "" # add hostname here | |
port = # add port (int) here | |
dbname = "" # add DB Name here | |
pswd = "" # add Password here | |
conn_string = get_conn_string_db(user,host,port,dbname,pswd) | |
verify_a_connection(conn_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment