gpt4 book ai didi

python - 操作系统错误 : [Errno 7] Argument list too long: 'php' - Python 3. 6

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:33:30 26 4
gpt4 key购买 nike

我想从我的 Python 脚本调用 PHP 脚本

我有这个代码

subprocess.run(['php', "script.php", long_string], stdout=subprocess.PIPE)

但是我得到了错误

OSError: [Errno 7] Argument list too long: 'php'

我已阅读在线帖子,我应该始终使用 subprocess.run for Python 3+

我也试过

subprocess.run(['ulimit', '-s', 'unlimited', 'php', "script.php", long_string], stdout=subprocess.PIPE, shell=True)

但是后来我明白了

OSError: [Errno 7] Argument list too long: '/bin/sh'

我的字符串是 141,664 个字符 = 141,706 个字节,而且还可以变大

我该怎么办?如何超越我的 Python 脚本的长度错误?

我的 uname -a 输出是

Linux mani 2.6.32-042stab123.9 #1 SMP Thu Jun 29 13:01:59 MSK 2017 x86_64 x86_64 x86_64 GNU/Linux

最佳答案

感谢@Andras Deak为了给我指明正确的方向,解决方案是从 STDIN 而不是命令行发送和读取数据

工作解决方案

Python代码

subprocess.run(['php', "script.php"], input=long_string.encode("utf-8"), stdout=subprocess.PIPE)

PHP代码

//to receive data from our Python scrapers
if (defined('STDIN')) {
$post = json_decode(fgets(STDIN), true);
}

旧代码(不工作)

Python代码

subprocess.run(['php', "script.php", long_string], stdout=subprocess.PIPE)

PHP代码

//to receive data from our Python scrapers
if (defined('STDIN')) {
$post = json_decode($argv[1], true);
}

关于python - 操作系统错误 : [Errno 7] Argument list too long: 'php' - Python 3. 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53612426/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com