简体   繁体   中英

Flask can't running in PHP

I want to run my Flask App Python in my PHP, with shell_exec command. but my Flask file is not running and not showing anything

I've type my script like this in streaming.php :

shell_exec("python C:/XAMPP/htdocs/device_management/page/streaming/main.py");

or this:

$command = escapeshellcmd('python C:/XAMPP/htdocs/device_management/page/streaming/main.py'); 
$output = shell_exec($command); 
echo $output;

My script Flask "main.py" is like this:

#!C:/Program Files/Python37-32/python.exe
import sys
import cv2
import numpy as np
from flask import Flask, request, render_template, Response, redirect, url_for
class VideoCamera(object):
def __init__(self):
    self.video = cv2.VideoCapture('rtsp://username:password@"192.168.1.18":554/Streaming/Channels/102')
    
def __del__(self):
    self.video.release()

def get_frame(self):
    success, image = self.video.read()
    cv2.resize(image, None, fx=1, fy=1, interpolation=cv2.INTER_AREA)
    ret, jpeg = cv2.imencode('.jpg', image)
    return jpeg.tobytes()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
def gen(VideoCamera):
while True:
    frame = VideoCamera.get_frame()
    yield (b'--frame\r\n'
           b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/camera_1')
def camera_1():
return Response(gen(VideoCamera()),
                mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='127.1.0.1', port=81, debug=True)

But, if i type my Python Flask directory in the URL like this: “http://localhost/device_management/page/streaming/main.py”

it's running and show Command Prompt It will showing Command Prompt and my flask app is running enter image description here

And if i type URL http://127.1.0.1:81 in the nex tab, I can see the CCTV is showing enter image description here


Actually, can i run Flask App wth shell_exec in PHP?

Windows path needs backslashes. Try:

shell_exec("python C:\\XAMPP\\htdocs\\device_management\\page\\streaming\\main.py");

Also check permissions. If it is already running, PHP may cannot start it again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM