Python에서 PHP 파일 실행하고 결과 받아오기

1 개요[ | ]

Python에서 PHP 파일 실행하고 결과 받아오기

2 기본 실행[ | ]

root@localhost# cat hello.php
<?php
for($i=0; $i<3; $i++) {
    echo "$i I'm PHP.\n";
}
root@localhost# python hello.py
0 I'm PHP.
1 I'm PHP.
2 I'm PHP.
root@localhost# cat executor.py
import os
result = os.popen('php hello.php').read().strip()
print( result )
root@localhost# python executor.py  
0 I'm PHP.
1 I'm PHP.
2 I'm PHP.

3 argument로 값 전달[ | ]

root@localhost# cat hello.php 
<?php
print_r($argv);
root@localhost# php hello.php 
Array
(
    [0] => hello.php
)
root@localhost# php hello.php lorem ipsum
Array
(
    [0] => hello.php
    [1] => lorem
    [2] => ipsum
)
root@localhost# cat executor.py
import os
result = os.popen('php hello.php from python').read().strip()
print( result )
root@localhost# python executor.py
Array
(
    [0] => hello.php
    [1] => from
    [2] => python
)

4 환경변수로 값 전달[ | ]

root@localhost# cat hello.php
<?php
echo 'foo: '.getenv('foo')."\n";
echo 'monkey: '.getenv('monkey')."\n";
root@localhost# php hello.php
foo: 
monkey:
root@localhost# foo=bar monkey=banana php hello.php 
foo: bar
monkey: banana
root@localhost# cat executor.py
import os
result = os.popen('foo=from monkey=python php hello.php').read().strip()
print( result )
root@localhost# python executor.py  
foo: from
monkey: python

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}