gpt4 book ai didi

php - 如何从 python 调用服务器中的 php 脚本

转载 作者:行者123 更新时间:2023-11-30 22:12:49 25 4
gpt4 key购买 nike

我正在尝试从本地计算机中的 python 项目运行服务器中的 php 脚本。

到目前为止我已经尝试过

Python 端:

# -*- coding: utf-8 -*-

import subprocess
import json
import sys
import os

def php(script_path):
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
result = p.communicate()[0]
return result

image_dimensions_json = str(php("http://XXX.XXX.XX.XX/logistic_admin/test1.php"))
dic = json.loads(image_dimensions_json)
print str(dic["0"]) + "|" + str(dic["1"])

PHP 端:

test1.php

<?php
echo(json_encode(getimagesize($argv[1])));

?>

但我面临以下错误:

Traceback (most recent call last):
File "D:\folder\test.py", line 20, in <module>
image_dimensions_json = str(php("http://XXX.XXX.XX.XX/logistic_admin/test1.php"))
File "D:\folder\test.py", line 16, in php
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
File "C:\Python27\Lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python27\Lib\subprocess.py", line 948, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

最佳答案

尝试使用urllib模块。

例如:

import urllib

def php(script_path):
urlData = urllib.urlopen(script_path)
return urlData.read()

关于php - 如何从 python 调用服务器中的 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50965278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com