使用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