使用Python使用公钥和cx_Oracle进行身份验证 [英] Authentication with public keys and cx_Oracle using Python
问题描述
我已经用Google搜索了一下,但没有发现任何实质性的结果.是否可以使用基于密钥的身份验证来使用Python连接到Oracle服务器?我的目标是能够自动执行我正在使用Python进行的某些报告,而不必在服务器中的任何位置存储用户名/密码.
I've Googled a bit but I haven't found any substantial results. Is it possible to use key-based authentication to connect to an Oracle server using Python? My objective is to be able to automate some reporting I'm doing with Python without having to store a username/password anywhere in the server.
推荐答案
一种可能的解决方案是实施Oracle Wallet.创建Oracle Wallet条目需要具备以下条件:
One possible solution is to implement Oracle Wallet. Creating an Oracle Wallet entry involves having:
- 为所述实例建立的tnsname解析名称
- 用户名和密码
示例:我正在使用的Oracle sid名为ORCL,必须与之连接的用户名为my_user.在您的tnsnames.ora文件中,您已经具有一个可以解析ORCL服务名称/sid的条目,并使用完全相同的参数创建一个条目:
Example: The Oracle sid I'm working with is named ORCL, the user I have to connect with is named my_user. In your tnsnames.ora file you already have an entry that resolves the ORCL service name/sid, create one more with exactly the same parameters:
#initial local name entry:
ORCL = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = my_ip)(PORT = 1528))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCL)))
#create an additional local name entry:
ORCL_MY_USER = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = my_ip)(PORT = 1528))) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = ORCL)))
新条目成功解析后,为ORCL_MY_USER本地名称创建oracle钱包条目.您将在python脚本中使用此新的本地名称进行连接,而无需在其中提供密码或对其进行硬编码.
After the new entry resolves successfully, create the oracle wallet entry for the ORCL_MY_USER local name. This new local name you're going to use in your python script to connect without providing or hard coding a password in it.
示例:
导入cx_Oracle
import cx_Oracle
db = cx_Oracle.connect("/@ ORCL_MY_USER")
db = cx_Oracle.connect("/@ORCL_MY_USER")
cursor = db.cursor()
cursor = db.cursor()
r = cursor.execute("SELECT用户名,密码, 来自的account_status,default_tablespace,temporary_tablespace dba_users按用户名排序")
r = cursor.execute("SELECT username, password, account_status, default_tablespace, temporary_tablespace from dba_users order by username")
用于用户名,密码,account_status,default_tablespace, 光标中的临时表空间:
for username, password, account_status, default_tablespace, temporary_tablespace in cursor:
print username, '\t', password, '\t', account_status, '\t', default_tablespace, '\t', temporary_tablespace
这篇关于使用Python使用公钥和cx_Oracle进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!