简体   繁体   English

从 html javascript 表单调用 python 程序

[英]Calling python program from html javascript form

I am trying to make a webpage where I enter a latitude and longitude and a python program will process the latitude and longitude in the form of a graph.我正在尝试制作一个网页,在其中输入纬度和经度,python 程序将以图形的形式处理纬度和经度。 For simplicity sake, in this question I just have the python program just repeat the latitude and longitude because I am wanting to know how to run the python program in the context of the html and javascript code and pass the arguments from the html form (latitude and longitude) into the python script.为简单起见,在这个问题中,我只让 python 程序重复纬度和经度,因为我想知道如何在 html 和 javascript 代码的上下文中运行 python 程序并从 html 表单(纬度和经度)到 python 脚本中。 Below is the html code:下面是html代码:

<html>

<head>

<script>

    function set_iterations()
    {
       var count = 0;
       var lat = document.getElementById("lat").value;
       var lon = document.getElementById("lon").value;
       document.write("latitude is " + lat + " and longitude is " + lon + "</br>"); 
      $.ajax({
         type: "POST"
         url: "testmaker.py" + lat + lon
       });
   }

</script>
</head>

<body>
       <td style='border-right:none' align='center'>Latitude: </td>
       <td style='border-right:none' align='center'><input type=text id='lat' size=10></td>
       <td style='border-right:none' align='center'>Longitude: </td>
       <td style='border-right:none' align='center'><input type=text id='lon', size=10></td>
       <br> 
       <button style="width:200px;margin-top:5px;margin-bottom:20px;"   onClick="set_iterations();">Submit</button>

</body>
</html>

The python program is this: python程序是这样的:

  #!/usr/bin/python

  import sys

  latitude = sys.argv[1]
  longitude = sys.argv[2]

  print "Using coordinates: ",latitude,",",longitude

How should I modify this program so that the submit button on the form would call the python program with the entries in the form being command line arguments for the python program?我应该如何修改这个程序,以便表单上的提交按钮调用 python 程序,表单中的条目是 python 程序的命令行参数?

"

You need a comma between arguments in your ajax call:您需要在 ajax 调用中的参数之间使用逗号:

$.ajax({     
    type: "POST",         
    url: "testmaker.py" + lat + lon
});

You need a web server of some sort.您需要某种网络服务器。 As a start, you can write a super simple server in Node:首先,您可以在 Node 中编写一个超级简单的服务器:

server.js
var http = require('http');
var fs = require('fs');
var index = fs.readFileSync('form.html');

http.createServer(function (req, res) {
  console.log("request made") ;
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end(index);
}).listen(9615);

Start it:启动它:

node server.js

Then go to http://127.0.0.1:9615/ from your browser然后从浏览器转到http://127.0.0.1:9615/

Now you need to do something in the server to respond to a call to your 'testmaker' url...现在您需要在服务器中做一些事情来响应对您的“testmaker”网址的调用...

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

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