如何从python在服务器中调用php脚本
我正在尝试从本地机器上的python项目运行服务器上的php脚本 到现在为止,我一直试着跟随 Python方面:如何从python在服务器中调用php脚本,php,python,python-2.7,subprocess,Php,Python,Python 2.7,Subprocess,我正在尝试从本地机器上的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.communic
# -*- 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])));
?>
test1.php
但我面临以下错误:
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
回溯(最近一次呼叫最后一次):
文件“D:\folder\test.py”,第20行,在
image_dimensions_json=str(php(“http://XXX.XXX.XX.XX/logistic_admin/test1.php"))
文件“D:\folder\test.py”,第16行,php格式
p=subprocess.Popen(['php',script_path],stdout=subprocess.PIPE)
文件“C:\Python27\Lib\subprocess.py”,第711行,在\uuu init中__
错误读取,错误写入)
文件“C:\Python27\Lib\subprocess.py”,第948行,在执行子进程中
startupinfo)
WindowsError:[错误2]系统找不到指定的文件
尝试使用urllib
模块
Ex:
import urllib
def php(script_path):
urlData = urllib.urlopen(script_path)
return urlData.read()
或者您可以使用请求模块:
import requests
def req(url):
r = requests.get(url)
return r.text
或urllib2模块:
import urllib2
def req(url):
f = urllib2.urlopen(url)
return f.read()
您可能需要使用urllib或请求module@Rakesh如何使用它。你能给我举个例子吗?为什么你要通过HTTP网络服务器调用你的PHP?PHP和Python是否都在同一台服务器上?@delboy1978uk不,我的PHP代码在服务器上,Python在我的桌面上。好的,很公平,谢谢@Rakesh