【发布时间】:2015-05-27 04:22:40
【问题描述】:
以前的研究:
- Running a Python script from PHP
- error on using exec() to call python script
- Php exec python script 'weakness'/downside
- Using exec() to run python script in PHP
我在 OS X 上运行 php 版本 5.6.6 和 python 版本 3.4.3。
基本上,我遇到的问题是,如果我通过命令行运行 python 脚本,它可以找到,但如果我通过 PHP 脚本运行它(使用exec()),我会收到此错误:
AttributeError: type object 'int' has no attribute 'from_bytes'
我创建并测试了一个微型隔离测试用例来显示问题。我已经完成了chmod 777 mypy.py 以确保 mypy.py 是可执行的。
myphp.php:
<?php
exec("/usr/bin/python mypy.py 1A", $output, $return);
var_dump($output);
mypy.py:
#!/usr/bin/env python
import string
import array
import binascii
import sys
if __name__ == "__main__":
hexval = sys.argv[1]
binval = binascii.unhexlify(hexval)
binint = int.from_bytes(binval, byteorder='big', signed=False)
print("int: " + str(binint))
(我知道有更好的方法来完成这个 python 脚本中所做的事情,我只是在制作一个会产生相同错误的测试用例)
当我通过命令行运行 python mypy.py 1F 时,我得到了打印:
int: 31
但是当我通过命令行运行php myphp.php 时,我得到了这个打印:
Traceback (most recent call last):
File "mypy.py", line 11, in <module>
binint = int.from_bytes(binval, byteorder='big', signed=False)
AttributeError: type object 'int' has no attribute 'from_bytes'
array(0) {
}
(注意:我还从 php 脚本中执行了whoami,只是为了验证我的普通用户是运行 python 脚本的用户)
【问题讨论】:
-
int没有方法.from_bytes,该代码不会在任何使用python2的地方运行,很确定你需要/usr/bin/python3 -
我刚试过你的测试用例。我遇到了与直接运行 .py 脚本时相同的错误。我正在运行 2.7。看起来
int.from_bytes>= 3.2?