使用flask实现提交表单至后台

本文介绍如何使用Flask框架创建一个简单的表单提交功能,将用户输入的IP地址和备注信息提交至阿里云SLB白名单。通过Python脚本与阿里云SDK交互,实现自动化管理SLB白名单的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用flask实现一个简单的提交表单至后台操作
本文后台以阿里云接口为例,前台填写表单提交至后台提交到阿里云SLB白名单

#!/usr/bin/env python
#coding=utf-8

#pip3 install aliyun-python-sdk-core
#pip3 install aliyun-python-sdk-slb
import sys
import json
import requests
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkslb.request.v20140515.AddAccessControlListEntryRequest import AddAccessControlListEntryRequest

def AclEntrys2(IP, rogin):
    AclEntrys2 = [{'entry': IP + '/32', 'comment': rogin}]
    Acljson = json.dumps(AclEntrys2)
    return Acljson

def addslb(AclEntrys,AclId='acl-xxxxxxxx'):
    client = AcsClient('access', 'access-key', 'cn-shanghai')
    request = AddAccessControlListEntryRequest()
    request.set_accept_format('json')
    request.set_AclId(AclId)
    request.set_AclEntrys(AclEntrys)
    add = client.do_action(request)
    return add

if __name__=='__main__':
    add1 = AclEntrys2(sys.argv[1],sys.argv[2])
    add2 = addslb(add1)

此事例是提交两个变量 IP和备注信息实现操作
python需要安装

from flask import Flask, render_template, request
import als
import requests
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/result', methods=['POST','GET'])
def result():
    if request.method == 'POST':
        IP=request.form.get('IP')
        rogin=request.form.get('rogin')
        test1=als.AclEntrys2(IP,rogin)
        addip1=als.addslb(test1)
        print (addip1)
        A = 'is not an ip address'
        B = 'The specified aclEntry already exists'
        a1 = A in str(addip1)
        a2 = B in str(addip1)
        if a1 == True:
            return 'IP格式不正确'
        elif a2 == True:
           return 'IP已存在'
        else:
            return 'IP已添加成功'
    else:
        return render_template('index.html')
if __name__ == '__main__':
    app.run(port=8808)

此脚本为flask接收表单操作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>添加SLB白名单</title>
</head>
<body>
    <h1>添加SLB白名单</h1>
    <form action="/result" method="post">
        <label>IP地址:</label><input type="text" name="IP" ><br/>
        <label>备注:</label><input type="text" name="rogin" ><br/>
        <button type="submit" value="提交">提交</button>
    </form>
</body>
</html>

前端页面
最终实现效果如图
在这里插入图片描述
附件资源可自行下载

https://download.youkuaiyun.com/download/w2909526/12664509

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪兽在此

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值