https如何使用python+flask来实现

生成证书与密钥
shell脚本

复制代码
#!/bin/bash

PROJECT_NAME=“https Project”

Generate the openssl configuration files.

cat > ca_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no

[ req_distinguished_name ]
O = $PROJECT_NAME Certificate Authority
EOF

cat > server_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no

[ req_distinguished_name ]
O = $PROJECT_NAME
CN =
EOF

cat > client_cert.conf << EOF
[ req ]
distinguished_name = req_distinguished_name
prompt = no

[ req_distinguished_name ]
O = $PROJECT_NAME Device Certificate
CN =
EOF

mkdir ca
mkdir server
mkdir client

生成私钥

openssl genrsa -out ca.key 1024
openssl genrsa -out server.key 1024
openssl genrsa -out client.key 1024

根据私钥创建证书请求文件,需要输入一些证书的元信息:邮箱、域名等

openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf
openssl req -out server.req -key server.key -new -config ./server_cert.conf
openssl req -out client.req -key client.key -new -config ./client_cert.conf

结合私钥和请求文件,创建自签署证书

openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.key
openssl x509 -req -in server.req -out server.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key
openssl x509 -req -in client.req -out client.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key

mv ca.crt ca.key ca/
mv server.crt server.key server/
mv client.crt client.key client/

rm *.conf
rm *.req
rm *.srl
复制代码
一些命令的解释

复制代码
openssl genrsa [-out filename] [-passout arg] [-des] [-des3] [-idea] [numbits]

选项说明:
-out filename:将生成的私钥保存至filename文件,若未指定输出文件,则为标准输出。
-numbits:指定要生成的私钥的长度,默认为1024。该项必须为命令行的最后一项参数。
-des|-des3|-idea:指定加密私钥文件用的算法,这样每次使用私钥文件都将输入密码,太麻烦所以很少使用。
-passout args:加密私钥文件时,传递密码的格式,如果要加密私钥文件时单未指定该项,则提示输入密码。传递密码的args的格式

openssl req -out ca.req -key ca.key -new -config ./ca_cert.conf
主要命令选项:
-new :说明生成证书请求文件
-key :指定已有的秘钥文件生成秘钥请求,只与生成证书请求选项-new 配合。
-out :指定生成的证书请求或者自签名证书名称

openssl x509 -req -in ca.req -out ca.crt -sha1 -days 5000 -signkey ca.key
openssl x509命令具以下的一些功能,例如输出证书信息,签署证书请求文件、生成自签名证书、转换证书格式等。
-in filename:指定证书输入文件,若同时指定了"-req"选项,则表示输入文件为证书请求文件,再使用"-signkey"提供自签署时使用的私钥。
-out filename:指定输出文件
-days: 指定证书的有效时间长短。缺省为30天
复制代码
四、安装flask
需要安装python 的 openssl 的类库,使用pip 安装

pip install pyOpenSSL
五、https单向认证握手流程

python实现

server端:

复制代码
from flask import Flask
app = Flask(name)

@app.route(’/login’)
def hello_world():
return ‘Hello World!’

if name == ‘main’:
app.run(host=“0.0.0.0”, port=8091, ssl_context=(‘server.crt’, ‘server.key’))
复制代码
客户端:

复制代码
import urllib.request

import ssl

if name == ‘main’:
CA_FILE = “ca.crt”
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.check_hostname = False
context.load_verify_locations(CA_FILE)
context.verify_mode = ssl.CERT_REQUIRED
try:
request = urllib.request.Request(‘https://127.0.0.1:8091/login’)
res = urllib.request.urlopen(request, context=context)
print(res.code)
print(res.read().decode(“utf-8”))
except Exception as ex:
print(“Found Error in auth phase:%s” % str(ex))
复制代码
六、https双向认证握手流程

python实现

客户端:

复制代码
from flask import Flask, request, Response
import json

app = Flask(name)
@app.route("/login")
def hello():
return “Hello World!”
@app.route(’/login1’, methods=[‘POST’])
def login():
username = request.form.get(“username”)
password = request.form.get(“password”)
login_config = {
“name”: “pwd1”
}
if username in login_config.keys():
if password == login_config[username]:
return Response(json.dumps(True), status=200, mimetype=‘application/json’)

return Response(json.dumps(False), status=200, mimetype='application/json')

if name == “main”:
app.run(host=“0.0.0.0”, port=8091, ssl_context=(‘server/server.crt’, ‘server/server.key’))
复制代码
客户端:

复制代码
import urllib.request

import ssl

if name == ‘main’:
CA_FILE = “ca.crt”

context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.check_hostname = False
context.load_verify_locations(CA_FILE)
context.verify_mode = ssl.CERT_REQUIRED
dict = {
    "username": "name",
    "password": "pwd1",
}
data = urllib.parse.urlencode(dict).encode('utf-8')
try:
    request = urllib.request.Request('https://127.0.0.1:8091/login')
    res = urllib.request.urlopen(request, context=context)
    print(res.code)
    print(res.read().decode("utf-8"))
except Exception as ex:
    print("Found Error in auth phase:%s" % str(ex))
try:
    request = urllib.request.Request('https://127.0.0.1:8091/login1', data=data, method='POST')
    res = urllib.request.urlopen(request, context=context)
    print(res.code)
    print(res.read().decode("utf-8"))
except Exception as ex:
    print("Found Error in auth phase:%s" % str(ex))
    USB Microphone  https://www.soft-voice.com/

Wooden Speakers https://www.zeshuiplatform.com/
亚马逊测评 www.yisuping.cn
深圳网站建设www.sz886.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Python Flask可以很方便地实现一个仓库管理系统。下面是一个简单的示例: 首先,需要安装Flask模块: ``` pip install flask ``` 接下来,创建一个名为`app.py`的Python文件,导入所需的模块: ```python from flask import Flask, render_template, request, redirect, url_for app = Flask(__name__) ``` 定义一个仓库的类,包含一些基本的属性和方法: ```python class Warehouse: def __init__(self, name, location): self.name = name self.location = location self.items = [] def add_item(self, item): self.items.append(item) def remove_item(self, item): self.items.remove(item) ``` 创建一个仓库对象,并在根目录下显示它的信息: ```python warehouse = Warehouse('仓库1', '北京') @app.route('/') def index(): return render_template('index.html', warehouse=warehouse) ``` 编写一个简单的表单,用于添加和删除仓库物品: ```html <!DOCTYPE html> <html> <head> <title>仓库</title> </head> <body> <h1>仓库信息</h1> <p>名称:{{ warehouse.name }}</p> <p>位置:{{ warehouse.location }}</p> <h2>物品列表</h2> <ul> {% for item in warehouse.items %} <li>{{ item }}</li> {% endfor %} </ul> <h2>添加物品</h2> <form action="/add" method="post"> <input type="text" name="item_name" placeholder="物品名称"> <input type="submit" value="添加"> </form> <h2>删除物品</h2> <form action="/remove" method="post"> <input type="text" name="item_name" placeholder="物品名称"> <input type="submit" value="删除"> </form> </body> </html> ``` 定义两个路由函数,用于处理添加和删除物品的请求: ```python @app.route('/add', methods=['POST']) def add_item(): item_name = request.form['item_name'] warehouse.add_item(item_name) return redirect(url_for('index')) @app.route('/remove', methods=['POST']) def remove_item(): item_name = request.form['item_name'] warehouse.remove_item(item_name) return redirect(url_for('index')) ``` 最后,运行应用: ```python if __name__ == '__main__': app.run() ``` 通过在浏览器中访问`http://localhost:5000`,即可看到仓库管理系统的界面,可以添加和删除仓库物品。 这只是一个简单的示例,仓库管理系统还可以进一步完善,例如添加权限控制、查询功能等等。但希望以上示例能够帮助您了解如何使用Python Flask实现仓库管理系统。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值