python加密通信的优化1.0

之前写过一个极其不完善的加密通信程序今天简单的优化一下。

之前的代码:

import socket
from cryptography.fernet import Fernet
 
# 生成加密密钥
key = Fernet.generate_key()
cipher_suite = Fernet(key)
 
# 服务器端代码
def server():
    host = '127.0.0.1'
    port = 12345
 
    # 创建socket对象
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(1)
 
    print("等待客户端连接...")
 
    # 接受客户端连接
    client_socket, addr = server_socket.accept()
    print("客户端已连接:", addr)
 
    while True:
        # 接收加密的消息
        encrypted_message = client_socket.recv(1024)
        decrypted_message = cipher_suite.decrypt(encrypted_message).decode()
 
        if decrypted_message == "结束":
            break
 
        print("客户端消息:", decrypted_message)
 
        # 发送加密的响应
        response = input("服务器回复:")
        encrypted_response = cipher_suite.encrypt(response.encode())
        client_socket.send(encrypted_response)
 
    client_socket.close()
    server_socket.close()
 
# 客户端代码
def client():
    host = '127.0.0.1'
    port = 12345
 
    # 创建socket对象
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((host, port))
 
    while True:
        # 发送加密的消息
        message = input("客户端消息:")
        encrypted_message = cipher_suite.encrypt(message.encode())
        client_socket.send(encrypted_message)
 
        if message == "结束":
            break
 
        # 接收加密的响应
        encrypted_response = client_socket.recv(1024)
        decrypted_response = cipher_suite.decrypt(encrypted_response).decode()
        print("服务器回复:", decrypted_response)
 
    client_socket.close()
 
# 启动服务器和客户端
server()
client()

___________________________________

修改后:

import socket
from cryptography.fernet import Fernet
 
# 生成加密密钥
key = Fernet.generate_key()
cipher_suite = Fernet(key)
 
# 服务器端代码
def server():
    host = input(输入IP以创建服务器)
    port = input(输入端口号以接收数据)
 
    # 创建socket对象
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(1)
 
    print("等待客户端连接...")
 
    # 接受客户端连接
    client_socket, addr = server_socket.accept()
    print("客户端已连接:", addr)
 
    while True:
        # 接收加密的消息
        encrypted_message = client_socket.recv(1024)
        decrypted_message = cipher_suite.decrypt(encrypted_message).decode()
 
        if decrypted_message == "结束":
            break
 
        print("客户端消息:", decrypted_message)
 
        # 发送加密的响应
        response = input("服务器回复:")
        encrypted_response = cipher_suite.encrypt(response.encode())
        client_socket.send(encrypted_response)
 
    client_socket.close()
    server_socket.close()
 
# 客户端代码
def client():
    host = input(输入IP以连接服务器)
    port = input(输入端口号以发送数据)
 
    # 创建socket对象
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect((host, port))
 
    while True:
        # 发送加密的消息
        message = input("客户端消息:")
        encrypted_message = cipher_suite.encrypt(message.encode())
        client_socket.send(encrypted_message)
 
        if message == "结束":
            break
 
        # 接收加密的响应
        encrypted_response = client_socket.recv(1024)
        decrypted_response = cipher_suite.decrypt(encrypted_response).decode()
        print("服务器回复:", decrypted_response)
 
    client_socket.close()
 
# 启动服务器和客户端
server()
client()

 

这次只是加入了选择服务器的功能,之后会加入客户端界面

你可能感兴趣的:(python,服务器)