从php运行python程序并获取输出

时间:2019-11-20 14:17:09

标签: php python

$command = escapeshellcmd('python3 /Users/thomasbruflot/Documents/nettsider/Curling/Auto.py');
$output = shell_exec($command);
echo $output;

我尝试了上面的代码从python脚本中获取一个名为“ lenke”的变量。我不明白的是我应该如何从python脚本发送变量。当前是这样的:

import matplotlib.pyplot as plt
import numpy as np
import pyimgur
import urllib.request as urllib2

f = open("/Users/thomasbruflot/Documents/nettsider/Curling/test.csv")

data = f.read()
data = data.split('\n')

data.remove(data[len(data)-1])
listexy = [data[i].split(',') for i in range(0,len(data))]

listex =[float(i[0]) for i in listexy]
listey = [float(i[1]) for i in listexy]

fig = plt.figure()
ax = plt.subplot()
plt.plot(listex,listey)
plt.title('Ditt støt')

fig.savefig('DetFunker.png')

CLIENT_ID = "2043cba6776e29d"
PATH = "/Users/thomasbruflot/Documents/nettsider/Curling/DetFunker.png"

im = pyimgur.Imgur(CLIENT_ID)
uploaded_image = im.upload_image(PATH, title="Uploaded with PyImgur")
print(uploaded_image.title)
print(uploaded_image.link) 

lenke = uploaded_image.link


req = urllib2.Request(url='http://localhost:8888/Curling/AutoPHP.php?lenke=%s' % uploaded_image.link )
f = urllib2.urlopen(req)
print(f.read())

1 个答案:

答案 0 :(得分:0)

我发现python3在sys中寻找错误的路径。对我来说起作用的是通过在脚本顶部添加来覆盖所有内容:

''' 导入系统 sys.path = 正确的路径 ''' 非常时髦的解决方案,因为我认为不必覆盖路径(既费时又不必要)。