【问题标题】:How to get php to send variable to python?如何让php将变量发送到python?
【发布时间】:2019-01-09 04:24:10
【问题描述】:

您好,我在 python 中创建了一个程序,当按下按钮时,它会播放音频文件并在 Raspberry Pi 上运行。我创建了一个网页,用户可以转到 Raspberry Pi 的 IP 地址并上传 MP3,然后他们可以选择他们想要播放的文件。有一些 PHP 代码查看目录以填写下拉列表。如何获取用户从下拉列表中选择的内容以导入 python 程序。这是 HTML/PHP 主页。

<html>
<body>
	<h1> Welcome to the Audio Setup </h1>
	<hr />
	<h3> Use to add mp3 file to server: </h3>
<form action="upload.php" method="post" enctype="multipart/form-data">
    Select MP3 to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
	<br />
	<br />
    <input type="submit" value="Upload MP3" name="submit">
</form>

<br />
<br />
<h3> Select file to play:
<hr />
<form name="sound" method="post" enctype="multipart/form-data" action="/home/pi/PushButton.py">
<label for="file">Select:</label>
<select name="sound">
      <option value=""> Select File</option>
<?php 
  $dirPath = dir('/var/www/html/uploads/');
	$imgArray = array();
	while (($file = $dirPath->read()) !== false) {
	    if ((substr($file, -3)=="mp3") || (substr($file, -3)=="MP3")) {
		   $imgArray[] = trim($file);
		}
    }
	$dirPath->close();
	sort($imgArray);
	$c = count($imgArray);
	for($i=0; $i<$c; $i++) {
	    echo "<option value=\"" . $imgArray[$i] . "\">" . 
		$imgArray[$i] . "\n";
	}	
?>
</select> 
</form>
</body>
</html>

这是它要进入的python程序。

import os
import sys
from time import sleep
import RPi.GPIO as GPIO
import mutagen
import cgi

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(23, GPIO.IN)

form = cgi.sound()
if form.getvalue() != null:
    musicFile = form.getvalue()
    timer = musicFile.info.length + 1
    while True:
        if GPIO.input(23) == False:
            os.system("omxplayer -o local /var/www/html/uploads/" + musicFile)
            sleep(timer);

我对编程还很陌生,从我研究的内容来看,cgi 是一种转移选择的方法,但可能不是最好的方法。如果这不是正确的方法,或者如果有更好的方法,请告诉我。

【问题讨论】:

    标签: php python html


    【解决方案1】:

    是的,你可以做到,但你需要在 python 端构建小 api,然后从 php 端的列表中选择文件后,你可以使用 php 向 python 的 api 发出请求

    【讨论】:

      【解决方案2】:

      一个非常简单的解决方案怎么样,希望它能满足您的要求。

      在您的 php 代码中,创建从选定文件到固定名称 (http://php.net/manual/en/function.symlink.php) 的符号链接,例如:/var/www/html/uploads/selected/1.mp3

      在您的 python 代码中,使用固定名称来检索选定的文件。

      干杯

      【讨论】:

        猜你喜欢
        • 2012-10-01
        • 2016-05-17
        • 2011-10-17
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 1970-01-01
        相关资源
        最近更新 更多