在本地服务器上使用Python脚本处理HTML表单

要在本地服务器上使用 Python 处理 HTML 表单,可以使用 Flask 框架,这是一个轻量级的 web 框架,特别适合快速构建和处理 HTTP 请求。

以下是如何使用 Flask 创建一个本地服务器,展示 HTML 表单并处理提交的数据。

在这里插入图片描述

1、问题背景

有一个托管在本地服务器(apache2)上的 HTML 页面,想要将一些数据发送给 Python 脚本并对其进行处理。但是,当使用表单的 action 标签时,页面不会导航到下一页;而当尝试使用 Jquery 时,脚本不会被执行。希望得到帮助,提前感谢。

HTML代码:

<body>
  <div id="Header"></div>
    <div id="wrapper">
    <form name="login-form" class="login-form" action="/cgi-bin/test.py" method="post" onsubmit="validate();return false;">
      <div class="header">
        <h1>Enter Your Credentials</h1>
        <span>URL,Admin,Tenant,Password</span>
      </div>
      <div class="content">
        <input id="Uname" name="url" type="text" class="input username" placeholder="Keystone URL" />
        <div class="user-icon"></div>
        <input id="Pname"name="admin" type="password" class="input password" placeholder="Admin" />
        <div class="pass-icon"></div>
        <input id="oPass"name="password" type="password" class="input password" placeholder="Password" />
        <div class="pass-icon"></div>
        <input id="tenant"name="tenant" type="password" class="input password" placeholder="Tenant" />
        <div class="pass-icon"></div>   
      </div>
      <div class="footer">
        <input type="submit" name="submit" value="Submit" class="button"/>
      </div>
    </form>
  </div>
</body>

JavaScript 函数:

function validate() {
    var un = document.getElementById("Uname").value;
    var valid = false;

    var unArray = ["http://192.168.1.102:5000/v2.0"];
    for (var i = 0; i < unArray.length; i++) {
        if ((un == unArray[i])) {
            valid = true;
            break;
        }
    }

    if (valid) {
        alert("Login was successful");
        location.href = 'HomePage.html';
        document.login-form.action='/cgi-bin/test.py';

        return false;
    } else {
        alert("Login was Unsuccessful");
        return false;
    }
}

Ajax 调用:

$.ajax({
  type: "POST",
  url: "/cgi-bin/test.py",
  data: "stuff_for_python="+document.getElementById("Uname").value,
  success: function(response) {
      alert(response);
  },
  error: function(data) {
      alert(data.responseText);
  }
});

Python 脚本:

#!/usr/bin/python
import cgi, cgitb
from StringIO import StringIO
import json
from io import BytesIO
import pycurl

cgitb.enable()
# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
url = form.getvalue('stuff_for_python')

print "Content-type: text/html\r\n\r\n\n"
print

print url
f = open('/home/stack/writing.txt','wb')
f.write(url)
f.close()

2、解决方案

问题的关键在于通过 post 传递的数据值。

  1. 尝试使用以下 Ajax 调用:
$.ajax(
{
   type: "POST",
   url: "/cgi-bin/test.py" ,
   data: {stuff_for_python: document.getElementById("Uname").value},
   success: function(response)
   {
       alert(response);
   },
   error: function(data)
   {
       alert(data.responseText);
   },
});
  1. 在 Python 脚本中,可以使用 form.getvalue() 方法来获取表单字段的值。

  2. 使用 cgi.FieldStorage() 实例来处理表单数据,并使用 getvalue() 方法来获取字段值。

  3. 可以使用 print 语句来在浏览器中打印输出。

  4. 使用 open() 函数来打开一个文件,并使用 write() 方法来写入数据。

修改后的代码如下:

#!/usr/bin/python
import cgi, cgitb
from StringIO import StringIO
import json
from io import BytesIO
import pycurl

cgitb.enable()
# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
url = form.getvalue('stuff_for_python')

print "Content-type: text/html\r\n\r\n\n"
print

print url
f = open('/home/stack/writing.txt','wb')
f.write(url)
f.close()

这个流程让我们在本地处理 HTML 表单并使用 Python 脚本进行数据处理。如果有其他需求或问题,可以继续讨论!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值