能不能帮我将这两段代码放在一个主程序中
时间: 2024-04-03 12:34:37 浏览: 20
当然可以,可以将两段代码合并成一个 Python 程序。以下是合并后的代码:
```python
import cv2
import socket
import numpy as np
import struct
import sqlite3
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.backends import default_backend
key = b'0123456789abcdef0123456789abcdef'
iv = b'0123456789abcdef'
# 连接到数据库
conn = sqlite3.connect('video.db')
cursor = conn.cursor()
# 创建videos表
cursor.execute("CREATE TABLE IF NOT EXISTS videos (id INTEGER PRIMARY KEY, path TEXT)")
# 视频保存路径
path = 'M:/img/xinxixitong.avi'
# 创建视频编码器
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(path, fourcc, 30, (640, 480))
# 解密函数
def dt_f(ciphertext, key, iv):
# 创建 AES 解密器
dt = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()).decryptor()
# 解密图像
padded_data = dt.update(ciphertext) + dt.finalize()
# 移除填充
unpadder = padding.PKCS7(128).unpadder()
unpadded_data = unpadder.update(padded_data) + unpadder.finalize()
return unpadded_data
# 服务端IP和端口号
HOST = '0.0.0.0' # 任意IP地址
PORT = 6666
# 创建套接字对象
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sk:
sk.bind((HOST, PORT))
sk.listen(5)
print('等待连接...')
# 接受连接请求
client_conn, addr = sk.accept()
with client_conn:
print('与', addr,'相连接')
data_buffer = b'' # 缓存数据
while True:
data = client_conn.recv(6)
#如果没有数据传输则停止
if not data:
break
size = struct.unpack('!I', data)[0]
# 接收到的帧数据
data = b''
while len(data) < size:
packet = client_conn.recv(size - len(data))
if not packet:
break
data += packet
# 解密帧
dted_f = dt_f(data, key, iv)
# 将解密后的 bytes 数据转换为图像帧
dted_f = np.frombuffer(dted_f, dtype=np.uint8).reshape(480, 640, 3)
# 显示解密后的帧
cv2.imshow("2021218000_dted_Server", dted_f)
# 将解密后的帧写入本地文件
out.write(dted_f)
if cv2.waitKey(1) in [ord('q'),27]:
out.release()
break
# 将视频保存路径保存到数据库中
cursor.execute('INSERT INTO videos (path) VALUES (?)', (path,))
conn.commit()
conn.close()
cv2.destroyAllWindows()
```
以上代码中,将两段代码整合到一起,先连接到数据库,创建 videos 表,然后创建视频编码器和套接字对象。在套接字对象的 while 循环中,先接收到数据帧,然后对数据帧进行解密,解密后显示出来,并将解密后的帧写入本地文件。最后将视频保存路径保存到数据库中,关闭连接和窗口。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)