作者lccf (~(⊙o⊙)~)
看板Python
標題[問題] php call python
時間Thu Apr 29 05:30:52 2021
各位大大好
想請教一下
如果我目前想在appserv環境下 寫一隻PHP 去執行python
有參考相關的做法
PHP端
test.php
<?php
$command = escapeshellcmd('python3 /usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>
Python端: test.py
fp = open("filename.txt", "a")
# 寫入 This is a testing! 到檔案
fp.write("This is a testing!")
# 關閉檔案
fp.close()
如果用以下執行時
http://127.0.0.1/test.php
會無法正常產生filename.txt
他會直接略過整隻python, 不知是否有其他相關的作法 感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.140.62.217 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1619645456.A.A0D.html
→ TitanEric: python interpreter路徑 以及檔案路徑要注意 04/29 09:22
推 single4565: 而且就算能正常執行,它也只能執行簡單的函式庫 04/29 23:27
→ TitanEric: 樓上如果虛擬環境路徑正確應該都可以執行 04/30 09:56
→ OrzOGC: 我只會弄個flask讓php去call ...QQ 04/30 11:26
推 TitanEric: 剛測了一下可以 除了路徑之外 也要注意你IP跟port對不 04/30 15:21
→ TitanEric: 對 04/30 15:21
→ lccf: 感謝各位大大 04/30 16:11
→ kenduest: 因為web服務是使用某個獨立帳號身分執行,所以執行外部 05/02 10:24
→ kenduest: 程式時候也會延續原本身分。若是python後續開檔的所在 05/02 10:24
→ kenduest: 目錄權限沒有允許寫入就好發生錯誤。 05/02 10:24