Python:使用子进程发送postgres密码
我在使用子进程发送密码时陷入了死胡同,我不知道出了什么问题。如果有任何提示,我将不胜感激 我的代码:Python:使用子进程发送postgres密码,python,postgresql,python-2.7,Python,Postgresql,Python 2.7,我在使用子进程发送密码时陷入了死胡同,我不知道出了什么问题。如果有任何提示,我将不胜感激 我的代码: os.system("sudo -u postgres -h " + db_host + " psql postgres -c \"\password\" ") os.system("sudo -u postgres -h " + db_host + " createuser " + db_user); print bcolors.WARNING, "Please enter your pass
os.system("sudo -u postgres -h " + db_host + " psql postgres -c \"\password\" ")
os.system("sudo -u postgres -h " + db_host + " createuser " + db_user);
print bcolors.WARNING, "Please enter your password for : " + db_user, bcolors.ENDC
str = "sudo -u postgres -h " + db_host + " psql postgres -c \"\password " + db_user + "\""
# os.system(str)
p = subprocess.Popen(['sudo','-u', 'postgres', '-h', db_host, 'psql', 'postgres', "-c", "\password", db_user],stdin=subprocess.PIPE, shell=False)
p.communicate(raw_input("Please give me a password!")
当使用
os.system(str)
时,一切正常,但我想从用户那里获取键入的密码,将来我想将密码存储在配置文件中。这可能吗?停止你正在做的事情,使用psycopg2
。请您几乎不需要向psql
付费。只需连接PostgreSQL的psycopg2
本机客户端驱动程序
如果您需要以超级用户身份连接,请修改pg_hba.conf
以允许您的用户进行md5
密码验证,并为postgres
用户设置密码
os.system(“sudo-u postgres-h”+db\u host+“psql postgres-c\”\password\)
这没有道理。您正在尝试执行命令-h
。您需要首先调用psql
sudo
试图避免从stdin读取密码。它关闭stdin并直接重新打开当前的tty,作为一种安全措施,以帮助阻止人们使用捕获密码的命令包装sudo
因此,它的目的是击败你正在试图做的事情
你可以:
- 在
中为sudo
设置psql
模式李>NOPASSWD
- 在
环境变量中,将sudo\u askpass
与askpass助手程序一起使用。您可以编写一个sudo-A
,通过为自己创建SUDO\u ASKPASS
从程序继承的文件描述符中读取密码pipe()
- 使用
禁用密码提示<如果需要密码,code>sudo将失败sudo-n
- 。。。或者最重要的是,不要使用
运行sudo
,只需使用psql
psycopg2