繁体   English   中英

OSError: [Errno 7] 参数列表太长:'php' - Python 3.6

[英]OSError: [Errno 7] Argument list too long: 'php' - Python 3.6

我想从我的 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 characters = 141,706 bytes并且也可以变大

我该怎么办? 如何超过我的 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);
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM