用python3.10编写一个redis数据发送工具

用python3.10编写一个redis数据发送工具,可发送string和hash类型的数据,在json中更改要发送的数据,下面这段是源码
import tkinter as tk
import redis
import json

def send_data():
    # 获取用户输入的IP、端口、消息头和JSON数据
    ip = ip_entry.get()
    port = port_entry.get()
    header = header_entry.get()
    header1 = header_entry1.get()
    dou = dou_entry.get()
    json_data = json_entry.get("1.0", tk.END)
    json_data1 = json_entry1.get("1.0", tk.END)
    # 检查IP、端口、消息头是否为空
    if not ip or not port or not header or not header1:
        status_label.config(text="请填写完整的信息!")
        return

    # 创建Redis连接
    if json_data.strip() != "":
        r = redis.Redis(host=ip, port=port)
        # 发送普通数据
        redis_key = header.format(0, 1, 2)
        r.set(redis_key, json_data)
        # 提示发送成功
        status_label.config(text="数据已成功发送到Redis!")

    # 发送哈希表类型的值
    if json_data1.strip() != "":
        r = redis.Redis(host=ip, port=port)
        for i in range(1, int(dou) + 1):
            # 构造Redis键
            redis_key1 = header1.format(0, 1, 2)
            print(r.type(redis_key1))
            # r.hset(redis_key, str(i), json_obj)
            # r.hset(redis_key, str(i),json.dumps(json_obj))
            r.hset(redis_key1,str(i),json_data1)
            # 提示发送成功
            status_label.config(text="数据已成功发送到Redis!")

# 创建主窗口
window = tk.Tk()
window.title("Redis数据发送工具")
window.geometry("500x850+400+50")
# 创建IP输入框
ip_label = tk.Label(window, text="IP:")
ip_label.pack()
ip_entry = tk.Entry(window)
ip_entry.insert(0, '127.0.0.1')
ip_entry.pack()

# 创建端口输入框
port_label = tk.Label(window, text="端口:")
port_label.pack()
port_entry = tk.Entry(window)
port_entry.insert(0,'6379')
port_entry.pack()

# 创建消息头输入框
header_label = tk.Label(window, text="string消息头:")
header_label.pack()
header_entry = tk.Entry(window, width=50)
header_entry.insert(0,'MixSys:P1:MixDev001:detail.CellGranlue:001:String')
header_entry.pack()

# 创建消息头输入框
header_label1 = tk.Label(window, text="Hash消息头:")
header_label1.pack()
header_entry1 = tk.Entry(window, width=50)
header_entry1.insert(0,'MixSys:P1:MixDev001:detail.CellStatus:001:Hash')
header_entry1.pack()

dou_label = tk.Label(window, text="斗个数:")
dou_label.pack()
dou_entry = tk.Entry(window)
dou_entry.insert(0,'4')
dou_entry.pack()
json_obj={
    "ChestId": 1   
        }]
}

# 创建JSON数据输入框
json_label = tk.Label(window, text="普通发送JSON数据:")
json_label.pack()
json_entry = tk.Text(window, height=20, width=50)
json_entry.pack()
json_entry.insert("1.0", json.dumps(json_obj))

json_obj1={
    "HopperError": 0,
    "HopperStatus": 1    
}

# 创建JSON1数据输入框
json_label1 = tk.Label(window, text="hash发送JSON1数据:")
json_label1.pack()
json_entry1 = tk.Text(window, height=20, width=50)
# json_entry1.insert("1.0", str(json_obj))
json_entry1.insert("1.0", json.dumps(json_obj1))
json_entry1.pack()

# 创建发送按钮
send_button = tk.Button(window, text="发送", command=send_data,width=10)
send_button.pack()

# 创建状态标签
status_label = tk.Label(window, text="")
status_label.pack()

# 运行主循环
window.mainloop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值