如何读取从 python 执行的 mysql 命令的错误内容输出

How can I read the error content outputs of mysql command executed from python

bash_fc = rf"mysql -u {source_user_name} -h {source_ipv4_addr} --password={source_db_password}"

当我对上述命令使用以下函数时,

from subprocess import PIPE,run
def cons_r(cmd):
  response = run(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
  return response.stdout
response = os.popen(bash_fc)

mysql:[警告] 在命令行界面上使用密码可能不安全。 mysql:未知 OS 字符集 'cp857'。 mysql:切换到默认字符集'utf8mb4'。 错误 1045 (28000):用户 'root'@'pc.mshome.net' 的访问被拒绝(使用密码:是)

我无法读取输出,有什么方法可以让我读取吗?

此更改解决了问题

from subprocess import PIPE, run, STDOUT
def cons_r(cmd):
  response = run(cmd, stdout=PIPE, stderr=STDOUT, universal_newlines=True, shell=True)
  return response.stdout

您可以从标准错误中读取错误

import subprocess as sp

ret = sp.run(['ls', '*.bad'], stderr=sp.PIPE,
             stdout=sp.PIPE, shell=True, encoding="cp857")

if ret.returncode == 0:
    print(ret.stdout)
else:
    print(ret.stderr)