-1

How can I read in database credentials from a text file in my Python script? When Grafana searches for the target, it should go to the text file and get the DB credentials and execute it.

import cx_Oracle
import pprint
con = cx_oracle.connect(user= "", password="", sid= "")
con= cur.cursor()
cur.execute("SELECT ****FROM TEMP")
rows = cur.fetchall()
columns = cur.description()

Is there a way I can pass those DB credentials to the script from my text file?

Mihai Chelaru
  • 7,614
  • 14
  • 45
  • 51
rohit rocckz
  • 19
  • 1
  • 6
  • I really can't work out what the skeleton code here has to do with the credentials? You can configure `Flask` via a `config.py` file that can pass the db credentials – roganjosh Dec 13 '18 at 00:10
  • Are you using an ORM? – roganjosh Dec 13 '18 at 00:12
  • @roganjosh i want to give a text file to the script with all the required DB credentials. so if i want to add a new DB connection i just want to give the credentials list in the text file – rohit rocckz Dec 13 '18 at 00:19
  • @roganjosh am sorry about flask but dont worry about flask it is a unused import. now i removed from the question. – rohit rocckz Dec 13 '18 at 00:20
  • You still haven't removed all the Flask references, it's not an unused import, you're launching a webapp. – roganjosh Dec 13 '18 at 00:21
  • @roganjosh in that config.py file can I give a list of DB credentials? do you have any resource how can i declare the config.py file in the code? so for the variables shoudl i use os.getenv() ? – rohit rocckz Dec 13 '18 at 00:23
  • @roganjosh actually i want to get the data from DB and show it in grafana so for that purpose i used Flask. my main goal is to give a list of DB credentials in a text file or yaml or any file to the script. when i execute the script it should pick that particular DB credentials from the file and execute it? I removed all the Flask related stuff – rohit rocckz Dec 13 '18 at 00:25
  • [The docs](http://flask.pocoo.org/docs/1.0/config/), [the variables in the docs](http://flask.pocoo.org/docs/1.0/config/#builtin-configuration-values), [the main 3rd party tutorial](https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis) and the [corresponding code base](https://github.com/miguelgrinberg/microblog/tree/v0.23). Also [our community page](https://github.com/sopython/sopython-site/tree/master/sopy) – roganjosh Dec 13 '18 at 00:36
  • But you look like you're not using an ORM, which would mainly involve `flask-sqlalchemy` so actually, you may not be able to configure your entire DB connection with flask configuration alone, since you're not actually using anything flask related to access the data – roganjosh Dec 13 '18 at 00:38
  • @roganjosh no, I am using the flask for a different use case. my first problem is how would I give a list of DB credentials from a text file to my script and how shoudl i decalre in the code that the credentials are in text file ? am using flask for graana – rohit rocckz Dec 13 '18 at 00:49
  • Have you looked into tutorials on reading and writing text files via Python? There's nothing mysterious going on with the fact you want to use them for a database. If all your criteria are on a single line separated by a space, open the file, `readlines()` the contents, access the credential strings by index. – roganjosh Dec 13 '18 at 00:52

1 Answers1

0

Try to use import sys and to read the arguments passed sys.argv which is a list. The parameters you passed start from the second (index 1)

An example is the following.

import sys

if __name__ == "__main__":
    print(type(sys.argv))
    if len(sys.argv) <2:
        print("Expecting file credentials")
        exit(1)
    f = open(sys.argv[1])
    for line in f:
        print(line[:len(line)-1]) #avoid printing new line char

Specify how your credential file is supposed to be to extract the fields.

EDIT: the input file is a JSON

import sys
import json

if __name__ == "__main__":
    #print(type(sys.argv))
    if len(sys.argv) <2:
        print("Expecting file credentials")
        exit(1)
    f = open(sys.argv[1])
    #for line in f:
    #    print(line[:len(line)-1]) #avoid printing new line char
    ###READ FROM JSON
    data = f.read()
    print("---------")
    print(data)
    content = json.loads(data)
    print(content["postgres"]["host"])
    print(content["postgres"]["password"])

Note one thing that it was giving me problems: you can read the file with f.read() once. If you call it again there is not anything to read.

roschach
  • 8,390
  • 14
  • 74
  • 124
  • can i use a JSON file like this with this structure { "postgres": { "user": "user", "password": "password", "host": "host", "port": "port", "database": "database" } } – rohit rocckz Dec 13 '18 at 01:14
  • Of course you have to install the `json` library if not already installed. You might use also a virtual environment – roschach Dec 13 '18 at 10:15