简体   繁体   English

如何从php获取字符串(或其他数据)并将其传递给python脚本?

[英]How can I get a string (or other data) from php and pass it to a python script?

I'm trying to do this (seemingly) simple little project on my raspberry pi. 我正在尝试在树莓派上执行这个(貌似)简单的小项目。 I have a 16x2 LCD display, and I want to be able to input some text from a webpage and have it show up on the LCD. 我有一个16x2的LCD显示屏,我希望能够从网页上输入一些文本并将其显示在LCD上。

I have this simple html webpage where I'm trying to get the input from a user: 我有一个简单的html网页,试图从用户那里获取输入:

<!DOCTYPE html>
<head>
    <title>Text Input</title>
</head>

<body>
    <form action="index.php">
        Line One:<br>
        <input type="text" name="line1"><br>
        Line Two:<br>
        <input type="text" name="line2"><br>
        <input type="submit" value="Submit">
    </form>

    <?php
    // runs script and inputs 2 lines
    ?>

</body>

And the python is fairly simple, where line1 and line2 would be replaced by whatever is input. 而且python很简单,其中line1和line2会被任何输入替换。

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os

line1 = "Line One"
line2 = "Line Two"

os.chdir('/home/pi/lcd')
import lcd
lcd.lcd_init()
lcd.lcd_byte(lcd.LCD_LINE_1, lcd.LCD_CMD)
lcd.lcd_string(line1,2)
lcd.lcd_byte(lcd.LCD_LINE_2, lcd.LCD_CMD)
lcd.lcd_string(line2,2)
lcd.GPIO.cleanup()

I just don't know how to get the information from the webpage to the python script. 我只是不知道如何从网页获取信息到python脚本。 I know that from php, you can use exec() to do commands, but even then, I'm not sure how you would get the data from the command line into the python script. 我知道可以从php中使用exec()来执行命令,但是即使如此,我仍不确定如何从命令行将数据获取到python脚本中。

I would suggest you look into how post and get values are retrieved in Python. 我建议您研究一下如何在Python中检索发布和获取值。 I am not sure why you would even use PHP with this since the HTML form could post to your Python script and have it handle parsing the get/post parameters. 我不确定为什么您还要使用PHP,因为HTML表单可以发布到您的Python脚本并让它处理解析get / post参数。

You can see how the get/post values are retrieved at this SO question: How are POST and GET variables handled in Python? 您可以在以下SO问题中看到如何获取get / post值: 如何在Python中处理POST和GET变量?

Whether you are using get or post is determined by the method in your form tag. 是否使用get或post取决于表单标签中的方法。

<form method="post" action="url/to/python/script.py">
Line One:<br/>
<input type="text" name="line1"><br>
Line Two:<br/>
<input type="text" name="line2"><br>
<input type="submit" value="Submit">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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