parent
883a758914
commit
18e16853eb
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,40 +1,96 @@ |
|||||||
from flask import Flask, render_template, request, Response |
from flask import Flask, render_template, request, Response |
||||||
from faker import Faker |
from faker import Faker |
||||||
import json, pika |
import json, pika, uuid |
||||||
|
import pymysql |
||||||
|
import settings |
||||||
|
|
||||||
app = Flask(__name__) |
app = Flask(__name__) |
||||||
|
|
||||||
def shop_random(n): |
def shop_random(): |
||||||
fake = Faker(locale='zh_CN') |
fake = Faker(locale='zh_CN') |
||||||
# 随机生成姓名、商品、价格、地址,并返回一个字典 |
# 随机生成姓名、商品、价格、地址,并返回一个字典 |
||||||
shop = {"response": "下单成功", "data": {fake.name(): [fake.word(), fake.pyint(), fake.address()] for i in range(n)}} |
shop = {uuid.uuid4().hex: [fake.name(), fake.word(), fake.pyint(), fake.address()]} |
||||||
return shop |
return shop |
||||||
|
|
||||||
def send_queue(data={}): |
def send_queue(data={}): |
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) |
connection = pika.BlockingConnection(pika.ConnectionParameters(settings.MQHost, settings.MQPort)) |
||||||
channel = connection.channel() |
channel = connection.channel() |
||||||
|
|
||||||
# 声明一个名为 'message_queue' 的队列 |
# 声明一个名为 'message_queue' 的队列 |
||||||
channel.queue_declare(queue='message_queue') |
channel.queue_declare(queue=settings.MQName) |
||||||
|
|
||||||
# 将消息发送到队列中 |
# 将消息发送到队列中 |
||||||
channel.basic_publish(exchange='', |
channel.basic_publish(exchange='', |
||||||
routing_key='message_queue', |
routing_key=settings.MQName, |
||||||
body=data) |
body=data) |
||||||
# 关闭连接 |
# 关闭连接 |
||||||
connection.close() |
connection.close() |
||||||
|
|
||||||
|
def read_queue(): |
||||||
|
# 建立与RabbitMQ服务器的连接 |
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(settings.MQHost, settings.MQPort)) |
||||||
|
channel = connection.channel() |
||||||
|
|
||||||
|
# 声明一个队列 |
||||||
|
channel.queue_declare(queue=settings.MQName) |
||||||
|
|
||||||
|
# 获取队列中的消息数量 |
||||||
|
queue_info = channel.queue_declare(queue=settings.MQName, passive=True) |
||||||
|
message_count = queue_info.method.message_count |
||||||
|
connection.close() |
||||||
|
return message_count |
||||||
|
|
||||||
@app.route('/', methods=["GET", "POST"]) |
@app.route('/', methods=["GET", "POST"]) |
||||||
def index(): |
def index(): |
||||||
if request.method == 'POST': |
if request.method == 'POST': |
||||||
num = int(request.form.get('shop', 100)) |
shops = [] |
||||||
data = shop_random(num) |
num = request.form.get('shop') if request.form.get('shop') != "" else '5' |
||||||
json_data = json.dumps(data, ensure_ascii=False) # 设置 ensure_ascii 为 False |
num = int(num) |
||||||
send_queue(json_data) |
for i in range(num): |
||||||
return Response(json_data, content_type='application/json; charset=utf-8') |
data = shop_random() |
||||||
return render_template("index.html") |
send_queue(json.dumps(data, ensure_ascii=False)) |
||||||
|
shops.append(data) |
||||||
|
return render_template("index.html", items=shops) |
||||||
|
return render_template("index.html", items=[]) |
||||||
|
|
||||||
|
@app.route('/read', methods=["GET","POST"]) |
||||||
|
def read(): |
||||||
|
connection = pika.BlockingConnection(pika.ConnectionParameters(settings.MQHost,settings.MQPort)) |
||||||
|
channel = connection.channel() |
||||||
|
|
||||||
|
# 声明一个名为 'message_queue' 的队列 |
||||||
|
channel.queue_declare(queue=settings.MQName) |
||||||
|
data = [] |
||||||
|
|
||||||
|
def callback(ch, method, properties, body): |
||||||
|
nonlocal m |
||||||
|
# 这里处理从队列中获取到的消息 |
||||||
|
conn = pymysql.connect(host=settings.DBHost, user=settings.DBUser, password=settings.DBPassword) |
||||||
|
cur = conn.cursor() |
||||||
|
cur.execute('create database if not exists shop default charset "utf8";') |
||||||
|
cur.execute('use shop;') |
||||||
|
cur.execute('create table if not exists shop(id varchar(100), name varchar(20), goods varchar(20), price int, address varchar(50));') |
||||||
|
k = eval(body.decode()).items() |
||||||
|
for key, value in k: |
||||||
|
cur.execute('insert into shop values(%s,%s,%s,%s,%s);', (key, value[0], value[1], value[2], value[3])) |
||||||
|
data.append(k) |
||||||
|
conn.commit() |
||||||
|
cur.close() |
||||||
|
ch.basic_ack(delivery_tag=method.delivery_tag) |
||||||
|
m -= 1 |
||||||
|
if m == 0: |
||||||
|
ch.stop_consuming() |
||||||
|
|
||||||
|
if request.method == 'POST': |
||||||
|
m = request.form.get('dshop') if request.form.get('dshop') else '5' |
||||||
|
m = int(m) |
||||||
|
print('1', m) |
||||||
|
channel.basic_consume(queue=settings.MQName, |
||||||
|
on_message_callback=callback, |
||||||
|
auto_ack=False) # 将 auto_ack 设置为 False |
||||||
|
channel.start_consuming() |
||||||
|
message_count = read_queue() |
||||||
|
return render_template("readQueue.html", shop_nums=message_count, items=data) |
||||||
|
|
||||||
if __name__ == '__main__': |
if __name__ == '__main__': |
||||||
app.run(host="0.0.0.0", port=80) |
app.run(host="0.0.0.0", port=80) |
@ -1,31 +0,0 @@ |
|||||||
import pika |
|
||||||
import pymysql |
|
||||||
|
|
||||||
# 建立到 RabbitMQ 的连接 |
|
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) |
|
||||||
channel = connection.channel() |
|
||||||
|
|
||||||
# 声明一个名为 'message_queue' 的队列 |
|
||||||
channel.queue_declare(queue='message_queue') |
|
||||||
|
|
||||||
def callback(ch, method, properties, body): |
|
||||||
# 这里处理从队列中获取到的消息 |
|
||||||
conn = pymysql.connect(host='localhost', user='root', password='123456') |
|
||||||
cur = conn.cursor() |
|
||||||
cur.execute('create database if not exists shop default charset "utf8";') |
|
||||||
cur.execute('use shop;') |
|
||||||
cur.execute('create table if not exists shop(name varchar(20), goods varchar(20), price int, address varchar(50));') |
|
||||||
|
|
||||||
for k,v in eval(body.decode()).get('data').items(): |
|
||||||
cur.execute('insert into shop values(%s,%s,%s,%s);', (k, v[0], v[1], v[2])) |
|
||||||
print(k, v[0], v[1], v[2]) |
|
||||||
conn.commit() |
|
||||||
cur.close() |
|
||||||
|
|
||||||
# 从 'message_queue' 队列中获取消息 |
|
||||||
channel.basic_consume(queue='message_queue', |
|
||||||
on_message_callback=callback, |
|
||||||
auto_ack=True) |
|
||||||
|
|
||||||
print('Waiting for messages. To exit press CTRL+C') |
|
||||||
channel.start_consuming() |
|
@ -0,0 +1,11 @@ |
|||||||
|
# rabbitmq 配置 |
||||||
|
MQName="message_queue" |
||||||
|
MQUser="guest" |
||||||
|
MQPassword="guest" |
||||||
|
MQHost="localhost" |
||||||
|
MQPort=5672 |
||||||
|
|
||||||
|
# mysql数据库配置 |
||||||
|
DBHost="localhost" |
||||||
|
DBUser="root" |
||||||
|
DBPassword="QianFeng@123" |
@ -0,0 +1,73 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html lang="en"> |
||||||
|
<head> |
||||||
|
<meta charset="UTF-8"> |
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
||||||
|
<title>发货</title> |
||||||
|
<style> |
||||||
|
body { |
||||||
|
text-align: center; |
||||||
|
margin-top: 20px; /* 添加顶部边距 */ |
||||||
|
} |
||||||
|
|
||||||
|
table { |
||||||
|
width: 80%; |
||||||
|
margin: 0 auto; |
||||||
|
border-collapse: collapse; |
||||||
|
border: 2px solid #000; |
||||||
|
} |
||||||
|
|
||||||
|
th, td { |
||||||
|
border: 1px solid #000; |
||||||
|
padding: 10px; |
||||||
|
} |
||||||
|
|
||||||
|
th { |
||||||
|
background-color: #f2f2f2; |
||||||
|
} |
||||||
|
|
||||||
|
button { |
||||||
|
padding: 10px 20px; |
||||||
|
background-color: #007bff; |
||||||
|
color: #fff; |
||||||
|
border: none; |
||||||
|
cursor: pointer; |
||||||
|
} |
||||||
|
|
||||||
|
button:hover { |
||||||
|
background-color: #0056b3; |
||||||
|
} |
||||||
|
</style> |
||||||
|
</head> |
||||||
|
<body> |
||||||
|
<form action="/read" method="post"> |
||||||
|
请输入批量发货的商品数量:<input type="number" name="dshop"> |
||||||
|
<button type="submit">点击发货</button> |
||||||
|
</form> |
||||||
|
<p>当前未处理货物: {{ shop_nums }}</p> |
||||||
|
<table> |
||||||
|
<thead> |
||||||
|
<tr> |
||||||
|
<th>用户编号</th> |
||||||
|
<th>用户名称</th> |
||||||
|
<th>商品名称</th> |
||||||
|
<th>商品金额</th> |
||||||
|
<th>收货地址</th> |
||||||
|
</tr> |
||||||
|
</thead> |
||||||
|
<tbody> |
||||||
|
{% for item in items %} |
||||||
|
<tr> |
||||||
|
{% for item_key,item_value in item %} |
||||||
|
<td>{{ item_key }}</td> |
||||||
|
<td>{{ item_value[0] }}</td> |
||||||
|
<td>{{ item_value[1] }}</td> |
||||||
|
<td>{{ item_value[2] }}</td> |
||||||
|
<td>{{ item_value[3] }}</td> |
||||||
|
{% endfor %} |
||||||
|
</tr> |
||||||
|
{% endfor %} |
||||||
|
</tbody> |
||||||
|
</table> |
||||||
|
</body> |
||||||
|
</html> |
Loading…
Reference in new issue