如何使用Python子进程从curl获取信息

2 投票
2 回答
3920 浏览
提问于 2025-04-16 09:13

我在Windows上使用curl.exe,但不幸的是,我无法从控制台获取数据。

我的Python代码大致是这样的:

tid = subprocess.Popen("C:/users/zero/Desktop/lm/curl.exe -i -H \"Cookie: login=" + userhash + "\" -F \"type=" + caid + "\" -F \"description=\" -F \"descr=" + cleaned + "\" -F \"filetype=2\" -F \"name=" + pavadinimas + "\" -F \"file=@" + namel + "\" -F \"tag=@" + nto + "\" http://www.isos.lt/upload.php")

我尝试使用process.returncode来获取信息,但得到的结果不正确。我能在命令提示符中看到这些信息,但不知道怎么获取。也许可以通过stdout=subprocess.PIPE和process.communicate()来获取这些信息?

编辑:

这个sukhbir发的命令帮了我。谢谢。

p = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]

Where command is your command.

2 个回答

1
p = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]

这里的 command 是你要输入的指令。

7

通过子进程使用curl是比较麻烦的做法。为了省事,建议你使用 urllib 或者 pycurl

[更新]

现在,requests 库是处理这类事情的最佳选择。

撰写回答