zoukankan      html  css  js  c++  java
  • 05-python简易聊天工具(shelve模块练习)

    需求:
    1.有两个用户,一个是mychat.py,另一个是youchat.py
    2.通过执自己的文件,可以看到对方输入的内容,实现连个命令行窗口之间聊天的功能.
    3.通过shelve 持久化写入和和读取功能实现这个功能
    思路:
    1、A用户输入内容通过shelve 持久化保存起来,共B用户读取,实现数据共享。
    2、B用户输入内容通过shelve 持久化保存起来,共A用户读取,实现数据共享。
    3、两个用户的程序文件全部放在一个目录,从而实现数据互通,在命令行模式运行两个程序后,回车可以读取内容。
    4、

    A用户程序代码:mychat.py

     1 # Author:Dwdar
     2 import shelve, datetime
     3 
     4 while True:
     5     # 读取对方数据
     6     d_alex = shelve.open('shelve_alex')  # 打开一个文件
     7     print(d_alex.get("info"))
     8     print(d_alex.get("date"))
     9     d_alex.close()
    10 
    11     # 我说话
    12     d_dwdar = shelve.open('shelve_dwdar')  # 打开一个文件
    13     choice = input("请输入:>>>")
    14     info = {"dwdar说": str(choice)}
    15     d_dwdar['info'] = info  # 持久化dict
    16     d_dwdar['date'] = datetime.datetime.now()
    17     d_dwdar.close()

    B用户程序代码:youchat.py

     1 # Author:Dwdar
     2 import shelve, datetime
     3 
     4 while True:
     5     # 读取对方数据
     6     d_dwdar = shelve.open('shelve_dwdar')  # 打开一个文件
     7     # print(d_dwdar.get("name"))
     8     print(d_dwdar.get("info"))
     9     print(d_dwdar.get("date"))
    10     d_dwdar.close()
    11 
    12     # 我说话
    13     d_alex = shelve.open('shelve_alex')  # 打开一个文件
    14     choice = input("请输入:>>>")
    15     info = {"Alex说": str(choice)}
    16     d_alex['info'] = info  # 持久化dict
    17     d_alex['date'] = datetime.datetime.now()
    18     d_alex.close()

    运行效果:

  • 相关阅读:
    大哥带我走渗透8--CSRF的应用
    大哥带我走渗透7----解析漏洞
    大哥带我走渗透6(下)---文件上传
    视频学习XSS
    大哥带我走渗透5--南方数据
    大哥带我走渗透4(中)----oracle报错注入
    大哥带我走渗透ii--时间盲注,布尔盲注
    SQL语言基础和数据库操作
    less-7
    less-6
  • 原文地址:https://www.cnblogs.com/dwdar/p/11708149.html
Copyright © 2011-2022 走看看