【问题标题】:QProcess does not work with "wget"QProcess 不适用于“wget”
【发布时间】:2017-11-20 22:18:22
【问题描述】:

我正在尝试执行这样的命令:

wget --user=abc --ask-password https://xxxxx.com/file.zip

那么我必须提供密码。

这段代码应该处理它:

connect(&checkFW, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
//QString command = "wget --user=abc--ask-password https://xxxxxxx.com";
//checkFW.start("sh",QStringList() << "-c" <<"cd ;"+command);
checkFW.start("wget", QStringList() << "--user=abc" << "--ask-password"<< "https://xxxxxx.com/file.zip");
if (!checkFW.waitForStarted()){
    qDebug()<<"Could not start ";
    return false;
}

QString cmd = "password\n";
checkFW.write(cmd.toLatin1());
checkFW.waitForFinished(5000);
QByteArray result = checkFW.readAll();
QString mystr(result);
qDebug()<<"output:"<< mystr;

我多次使用 QProcess,但这次我无法使用它。一些建议?我尝试了不同的方法和任何响应。当然我要求的文件没有下载。


我再次检查,文件被下载到 /home 目录,而不是应用程序的目录。所以它可以工作,但是没有输出。

【问题讨论】:

  • 您假设wget 将从进程的标准输入中读取密码。我怀疑情况并非如此。 wget 可能委托给getpass 之类的东西来获取密码。反过来,这将在 /dev/tty 上执行显式 open 并从中读取而不是标准输入。

标签: c++ linux qt qprocess


【解决方案1】:

您以错误的方式向 QProcess 传递参数。

checkFW.start("wget", QStringList() << "--user=abc" << "--ask-password"<< "https://xxxxxx.com/file.zip");

这是 ping (Windows) 的示例。

#include <QCoreApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    QProcess proc;
    QStringList args;
    args << "google.com";
    args << "-n";
    args << "3";

    proc.start("ping", args);
    proc.waitForFinished();
    qDebug() << proc.readAllStandardOutput();
}

【讨论】:

    【解决方案2】:

    如果你只是把:

    checkFW.setProcessChannelMode(QProcess::MergedChannels);
    

    之前:

     checkFW.start();
    

    即使是错误,您也会在标准输出中获得所有输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 2020-08-16
      • 2012-07-22
      • 2017-12-04
      • 1970-01-01
      相关资源
      最近更新 更多