2014-11-23 71 views
0

我有一个表格,我通过html进行输入经度和纬度,然后提交这些信息。现在我想要我在html表单中输入的经度和纬度,用于调用python脚本。表单中的每个元素是叫python脚本的命令行参数:从html传递信息到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?

+0

你需要编码url – Hackaholic 2014-11-23 23:36:20

+0

这是什么意思? – jms1980 2014-11-23 23:59:51

+0

你能给网站链接吗? – Hackaholic 2014-11-24 00:01:18

回答

0

您需要运行Python Web服务器。一个简单的方法是安装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 
+0

这看起来不错,但它与我已有的脚本(包括python.py和我发布的html脚本)是如何链接的。我对此很新,需要一步一步的解释如何配合你在这里编写的链接我在这里写的html脚本到python脚本python.py? – jms1980 2014-11-24 04:18:02