"PHP에서 Python 파일 실행하고 결과 받아오기"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
3번째 줄: 3번째 줄:


==기본 실행==
==기본 실행==
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat hello.py
root@localhost# cat hello.py
for i in range(3):
for i in range(3):
     print(i, 'I'm Python.')
     print(i, 'I'm Python.')
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# python hello.py
root@localhost# python hello.py
0 I'm Python.
0 I'm Python.
1 I'm Python.
1 I'm Python.
2 I'm Python.
2 I'm Python.
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat executor.php  
root@localhost# cat executor.php  
<?php
<?php
20번째 줄: 20번째 줄:
$result = implode("\n", $arr)."\n";
$result = implode("\n", $arr)."\n";
echo $result;
echo $result;
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# php executor.php  
root@localhost# php executor.php  
0 I'm Python.
0 I'm Python.
1 I'm Python.
1 I'm Python.
2 I'm Python.
2 I'm Python.
</source>
</syntaxhighlight>


==argument로 값 전달==
==argument로 값 전달==
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat hello.py
root@localhost# cat hello.py
import sys
import sys
for arg in sys.argv:
for arg in sys.argv:
     print('arg:', arg)
     print('arg:', arg)
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# python hello.py
root@localhost# python hello.py
arg: hello.py
arg: hello.py
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# python hello.py alice bob
root@localhost# python hello.py alice bob
arg: hello.py
arg: hello.py
arg: alice
arg: alice
arg: bob
arg: bob
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat executor.php
root@localhost# cat executor.php
<?php
<?php
51번째 줄: 51번째 줄:
$result = implode("\n", $arr)."\n";
$result = implode("\n", $arr)."\n";
echo $result;
echo $result;
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# php executor.php
root@localhost# php executor.php
arg: hello.py
arg: hello.py
arg: from
arg: from
arg: php
arg: php
</source>
</syntaxhighlight>


==환경변수로 값 전달==
==환경변수로 값 전달==
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat hello.py
root@localhost# cat hello.py
import os
import os
print('foo:', os.getenv('foo'))
print('foo:', os.getenv('foo'))
print('monkey:', os.getenv('monkey'))
print('monkey:', os.getenv('monkey'))
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# python hello.py
root@localhost# python hello.py
foo: None
foo: None
monkey: None
monkey: None
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# foo=bar monkey=banana python hello.py
root@localhost# foo=bar monkey=banana python hello.py
foo: bar
foo: bar
monkey: banana
monkey: banana
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# cat executor.php
root@localhost# cat executor.php
<?php
<?php
82번째 줄: 82번째 줄:
$result = implode("\n", $arr)."\n";
$result = implode("\n", $arr)."\n";
echo $result;
echo $result;
</source>
</syntaxhighlight>
<source lang='console'>
<syntaxhighlight lang='console'>
root@localhost# php executor.php  
root@localhost# php executor.php  
foo: from
foo: from
monkey: php
monkey: php
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:31 기준 최신판

1 개요[ | ]

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

2 기본 실행[ | ]

root@localhost# cat hello.py
for i in range(3):
    print(i, 'I'm Python.')
root@localhost# python hello.py
0 I'm Python.
1 I'm Python.
2 I'm Python.
root@localhost# cat executor.php 
<?php
exec('python hello.py', $arr);
$result = implode("\n", $arr)."\n";
echo $result;
root@localhost# php executor.php 
0 I'm Python.
1 I'm Python.
2 I'm Python.

3 argument로 값 전달[ | ]

root@localhost# cat hello.py
import sys
for arg in sys.argv:
    print('arg:', arg)
root@localhost# python hello.py
arg: hello.py
root@localhost# python hello.py alice bob
arg: hello.py
arg: alice
arg: bob
root@localhost# cat executor.php
<?php
exec('python hello.py from php', $arr);
$result = implode("\n", $arr)."\n";
echo $result;
root@localhost# php executor.php
arg: hello.py
arg: from
arg: php

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

root@localhost# cat hello.py
import os
print('foo:', os.getenv('foo'))
print('monkey:', os.getenv('monkey'))
root@localhost# python hello.py
foo: None
monkey: None
root@localhost# foo=bar monkey=banana python hello.py
foo: bar
monkey: banana
root@localhost# cat executor.php
<?php
exec('foo=from monkey=php python hello.py', $arr);
$result = implode("\n", $arr)."\n";
echo $result;
root@localhost# php executor.php 
foo: from
monkey: php

5 같이 보기[ | ]

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