将信息从html传递到python脚本

2024-04-20 11:07:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表单,我通过html在那里输入经度和纬度,然后提交这个信息。现在我希望在html表单中输入的纬度和经度在python脚本中使用。表单中的每个元素都是一个命令行参数,称为python脚本:

./python.py

如何调用python脚本Python.py根据我在网站上提交的经纬度信息,使用上面指定的适当参数。下面是一段html代码。你知道吗

<center>Please Enter a Longitude and Latitude of the point where you want to look     at</center>
<center>(Longitudes West and Latitudes South should be entered as negative numbers i.e 170W is -170).</center>
<br></br>
<form>
<center>
Longitude: <br>
<input type="text" name="Longitude" />
<br>
Latitude: <br>
<input type="text" name="Latitude" />
<br>
<input type="submit" name="submit" value="Submit" />
</center>
</form>
</body>
</html>

我应该在这里添加什么来调用html文件/Python.py点击提交按钮?你知道吗


Tags: namepybr脚本信息表单input参数
1条回答
网友
1楼 · 发布于 2024-04-20 11:07:29

您需要运行pythonweb服务器。一个简单的方法是安装Flask库。例如:

from flask import Flask, request
app = Flask(__name__)

@app.route('/runscript', methods=['POST'])
def my_script():
    lat = request.form.get('lat')
    lon = request.form.get('lon')
    return "You submitted: lat=%s long=%s" % (lat,lon)

if __name__ == '__main__':
    app.run()

现在从命令行运行web服务器:

$ python myscript.py
 * Running on http://127.0.0.1:5000/

您可以将POST请求提交给http://127.0.0.1:5000/runscript以查看结果。我刚刚使用curl从命令行提交了一个请求:

$ curl -X POST  data "lat=1&lon=2" http://127.0.0.1:5000/runscript
You submitted: lat=1 long=2

相关问题 更多 >