parent
ef777f4026
commit
9e5bbc3a3a
@ -0,0 +1,51 @@
|
||||
# -*- coding: utf-8 -*-# -*- coding: utf-8 -*-
|
||||
# 订阅者
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
broker = '121.40.129.71'
|
||||
|
||||
# 连接成功回调
|
||||
def on_connect(client, userdata, flags, rc,properties):
|
||||
if rc.is_failure:
|
||||
print(f"连接失败: {rc}. loop_forever()将重新连接")
|
||||
else:
|
||||
print(f'连接成功: {rc}')
|
||||
client.subscribe('host_status/#')
|
||||
|
||||
|
||||
def on_subscribe(client, userdata, mid, reason_code_list, properties): # 订阅回调
|
||||
if reason_code_list[0].is_failure:
|
||||
print(f'订阅失败: {reason_code_list[0]}')
|
||||
else:
|
||||
print(f'订阅成功: {reason_code_list[0].value}')
|
||||
|
||||
def on_unsubscribe(client, userdata, mid, reason_code_list, properties): # 取消订阅回调
|
||||
if len(reason_code_list) == 0 or not reason_code_list[0].is_failure:
|
||||
print("取消订阅成功(if SUBACK is received in MQTTv3 it success)")
|
||||
else:
|
||||
print(f"取消订阅失败: {reason_code_list[0]}")
|
||||
client.disconnect() # 断开连接
|
||||
|
||||
# 消息接收回调
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
|
||||
mqtt = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="book_1")
|
||||
mqtt.on_connect = on_connect
|
||||
mqtt.on_message = on_message
|
||||
mqtt.on_subscribe = on_subscribe
|
||||
mqtt.on_unsubscribe = on_unsubscribe
|
||||
mqtt.is_connected()
|
||||
|
||||
mqtt.user_data_set(["user_data", "book_1"])
|
||||
mqtt.connect(broker, 48835, 60)
|
||||
|
||||
mqtt.loop_forever()
|
||||
print(f"收到以下消息: {mqtt.user_data_get()}")
|
||||
# while True:
|
||||
# rc = mqtt.loop(timeout=1.0)
|
||||
# print(11)
|
||||
# if rc != 0:
|
||||
# mqtt.reconnect()
|
||||
|
||||
|
@ -0,0 +1,32 @@
|
||||
# MQTT订阅客户端
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
broker = "121.40.129.71"
|
||||
|
||||
# 连接成功回调
|
||||
def on_connect(client, userdata, flags, rc,properties):
|
||||
print('Connected with result code '+str(rc))
|
||||
client.subscribe('tts/#')
|
||||
|
||||
# 消息接收回调
|
||||
def on_message(client, userdata, msg):
|
||||
print(msg.topic+" "+str(msg.payload))
|
||||
|
||||
|
||||
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, clean_session=True)
|
||||
|
||||
# username = "gd32"
|
||||
# password = "educoder"
|
||||
# client.username_pw_set(username, password)
|
||||
# 指定回调函数
|
||||
client.on_connect = on_connect
|
||||
client.on_message = on_message
|
||||
|
||||
# 建立连接
|
||||
client.connect(broker, 48835, 60)
|
||||
# 发布消息
|
||||
|
||||
# client.publish('emqtt',payload='Hello World',qos=0)
|
||||
|
||||
client.loop_forever()
|
@ -0,0 +1,93 @@
|
||||
更换为国内源可以提高软件包下载速度。常用的国内源有中科大、清华大学、阿里云等。下面是将 Debian Bookworm 的源替换为国内源的步骤:
|
||||
|
||||
### 步骤:
|
||||
|
||||
1. **备份当前源列表(可选):**
|
||||
|
||||
```bash
|
||||
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
|
||||
```
|
||||
|
||||
2. **编辑 `sources.list` 文件:**
|
||||
|
||||
使用你喜欢的文本编辑器(例如 `nano`、`vim` 或 `gedit`)编辑 `/etc/apt/sources.list` 文件:
|
||||
|
||||
```bash
|
||||
sudo nano /etc/apt/sources.list
|
||||
```
|
||||
|
||||
3. **更新源列表内容:**
|
||||
|
||||
下面是使用清华大学的镜像源作为示例,可以将其内容粘贴到 `sources.list` 文件中:
|
||||
|
||||
```plaintext
|
||||
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
||||
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
|
||||
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
||||
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
|
||||
#deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
```
|
||||
|
||||
其他可用的国内源示例:
|
||||
|
||||
**中科大镜像源:**
|
||||
|
||||
```plaintext
|
||||
deb http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
||||
deb http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
||||
deb http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.ustc.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.ustc.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
```
|
||||
|
||||
**阿里云镜像源:**
|
||||
|
||||
```plaintext
|
||||
deb http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
|
||||
deb http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
||||
deb http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.aliyun.com/debian/ bookworm main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.aliyun.com/debian-security/ bookworm-security main contrib non-free non-free-firmware
|
||||
#deb-src http://mirrors.aliyun.com/debian/ bookworm-updates main contrib non-free non-free-firmware
|
||||
```
|
||||
|
||||
4. **保存并退出编辑器:**
|
||||
|
||||
- 如果使用 `nano`,按 `Ctrl+O` 保存文件,然后按 `Ctrl+X` 退出编辑器。
|
||||
|
||||
5. **更新软件包列表:**
|
||||
|
||||
运行以下命令来更新软件包列表:
|
||||
|
||||
```bash
|
||||
sudo apt-get update
|
||||
```
|
||||
|
||||
### 验证更新
|
||||
|
||||
在运行 `sudo apt-get update` 后,你应该看到类似如下的输出,表示成功从新的软件源获取了软件包列表:
|
||||
|
||||
```plaintext
|
||||
Hit:1 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm InRelease
|
||||
Hit:2 https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security InRelease
|
||||
Hit:3 https://mirrors.tuna.tsinghua.edu.cn/debian bookworm-updates InRelease
|
||||
Reading package lists... Done
|
||||
```
|
||||
|
||||
### 安装和升级软件包
|
||||
|
||||
如果需要升级所有已安装的软件包,可以运行:
|
||||
|
||||
```bash
|
||||
sudo apt-get upgrade
|
||||
```
|
||||
|
||||
或者安装特定软件包:
|
||||
|
||||
```bash
|
||||
sudo apt-get install <package-name>
|
||||
```
|
||||
|
||||
通过这些步骤,你可以使用国内镜像源加快软件包的下载速度。
|
Loading…
Reference in new issue