Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:使用子进程发送postgres密码_Python_Postgresql_Python 2.7 - Fatal编程技术网

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
    环境变量中,将
    sudo-A
    与askpass助手程序一起使用。您可以编写一个
    SUDO\u ASKPASS
    ,通过为自己创建
    pipe()
    从程序继承的文件描述符中读取密码
  • 使用
    sudo-n
    禁用密码提示<如果需要密码,code>sudo将失败
  • 。。。或者最重要的是,不要使用
    sudo
    运行
    psql
    ,只需使用
    psycopg2