Vijay
Vijay

Reputation: 41

Connecting to oracle db from python

We are connecting to oracle from python using cx_oracle package. But the user_id, password and SID details are hardcoded in that. My question is, is there any way to create a Datasource kind of thing? Or how we will deploy such python script sin production?

The database is in a Linux box and python is installed in another Linux box(Weblogic server is also installed in this Linux box).

import cx_Oracle
con = cx_Oracle.connect('pythonhol/[email protected]/orcl')
print con.version

Expectation is :

Can we deploy python in a production instance? If yes how can we connect to the database by hiding the DB credentials?

Upvotes: 0

Views: 947

Answers (1)

Christopher Jones
Christopher Jones

Reputation: 10721

Use some kind of 'external authentication', for example a wallet. See the cx_Oracle documentation https://cx-oracle.readthedocs.io/en/latest/user_guide/connection_handling.html#connecting-using-external-authentication

In summary:

  • create a wallet with mkstore which contains the username/password credentials.
  • copy the wallet to the machines that are running Python
  • make sure no bad people can access the wallet
  • configure Oracle Net files to point to the wallet
  • your scripts would connect like

    connection = cx_Oracle.connect(dsn="mynetalias", encoding="UTF-8")
    

    or

    pool = cx_Oracle.SessionPool(externalauth=True, homogeneous=False, dsn="mynetalias",
                     encoding="UTF-8")
    pool.acquire()
    

Upvotes: 1

Related Questions