Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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测试与MySQL的连接_Python_Mysql_Bash_Subprocess - Fatal编程技术网

使用子流程使用Python测试与MySQL的连接

使用子流程使用Python测试与MySQL的连接,python,mysql,bash,subprocess,Python,Mysql,Bash,Subprocess,我试图使用subprocess和Bash命令在Python脚本中测试与MySQL的连接。在尝试连接和发送更多MySQL命令之前,我想确保用户提供的密码是正确的。我知道通过导入MySQLdb我可以很容易地做到这一点,但我想学习如何实现这一点 这就是我所拥有的: root_password = getpass.getpass("Enter the password of the database root user: ") try: subprocess.check_call("mysql -

我试图使用subprocess和Bash命令在Python脚本中测试与MySQL的连接。在尝试连接和发送更多MySQL命令之前,我想确保用户提供的密码是正确的。我知道通过导入MySQLdb我可以很容易地做到这一点,但我想学习如何实现这一点

这就是我所拥有的:

root_password = getpass.getpass("Enter the password of the database root user: ")
try:
   subprocess.check_call("mysql -u root -p%s" % root_password, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT)
except:
   print "Incorrect password provided."
密码正确后,stdin将挂起等待进一步提示,但我希望它退出,以便我可以发出以下消息:

mysql_cmd = 'mysql -u root -p{0} -Bse "CREATE DATABASE {1}; CREATE USER {2}@localhost IDENTIFIED BY \'{3}\'; GRANT SELECT, CREATE, INSERT, UPDATE, DELETE, ALTER, DROP, LOCK TABLES ON {1}.* TO {2}@localhost IDENTIFIED BY \'{3}\'; FLUSH PRIVILEGES;"'.format(root_password, database_name, database_user, database_password)
subprocess.call(mysql_cmd, shell=True, stdout=open('/dev/null', 'w'), stderr=subprocess.STDOUT)
是的,你猜对了;我正在尝试将Bash脚本转换为Python

来自:


如果
mysql
命令在密码正确时显示提示,则可以使用
Popen.communicate()
发送
exit
命令:

from subprocess import Popen, PIPE, DEVNULL, STDOUT

p = Popen(["mysql", "-u", "root", "-p", password],
          stdin=PIPE, stdout=DEVNULL, stderr=STDOUT)
p.communicate(b"exit")
if p.returncode != 0:
    print('incorrect password')

请记住中提到的安全含义。

如果要通过命令行传递密码,可能必须正确转义密码。一般来说,这是一个非常非常糟糕的主意,因为系统中的每个用户都可能在登录过程中使用密码观察您的命令。密码不是问题,因为命令成功通过并返回mysql提示。我只想测试一下密码是否正确,而不是返回mysql提示符。这就是我要找的。
from subprocess import Popen, PIPE, DEVNULL, STDOUT

p = Popen(["mysql", "-u", "root", "-p", password],
          stdin=PIPE, stdout=DEVNULL, stderr=STDOUT)
p.communicate(b"exit")
if p.returncode != 0:
    print('incorrect password')