- 网络 udp
- 多任务 线程
- 多任务 线程、进程
- 网络 tcp
- 飞鸽传书 完善
- 多任务 协程
- 正则表达式
- 网络通信过程、http 协议
- Web 服务器 并发服务器
- WSGI、mini Web 框架
- 装饰器、mini Web 框架 路由
- MySQL 基本使用
- MySQL 查询
- MySQL 与 Python 交互
- mini Web 框架 添加 MySQL 功能
- 其它知识
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
飞鸽传书 多任务聊天
飞鸽传书-多任务聊天
main.py(用来控制整体)
import threading
import socket
import time
import FeiQCoreData
import FeiQRecv
import FeiQSend
def create_udp_socket():
"""创建udp套接字"""
FeiQCoreData.udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 设置允许广播
FeiQCoreData.udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
def print_menu():
"""显示飞鸽传书的功能"""
print(" 飞鸽传书v1.0")
print("1:上线广播")
print("2:下线广播")
print("3:向指定ip发送消息")
print("0:退出")
def main():
# 创建套接字
create_udp_socket()
# 创建另外一个线程,循环接收udp数据
recv_msg_thread = threading.Thread(target=FeiQRecv.recv_msg)
recv_msg_thread.start()
while True:
print_menu()
command_num = input("请输入要进行的操作:")
if command_num == "1":
# 发送上线提醒
FeiQSend.send_broadcast_online_msg()
elif command_num == "2":
# 发送下线提醒
FeiQSend.send_broadcast_offline_msg()
elif command_num == "3":
# 向指定ip发送消息
FeiQSend.send_msg_2_ip()
elif command_num == "0":
FeiQSend.send_broadcast_offline_msg()
# 关闭套接字
FeiQCoreData.udp_socket.close()
exit()
if __name__ == "__main__":
main()
FeiQRecv.py(用来存放收数据相关功能)
import FeiQCoreData
import FeiQSend
def deal_feiq_data(recv_data):
"""处理接收到的飞秋数据"""
# b'1_lbt80_0#128#000C29770BAB#0#0#0#4000#9:1501336422:Administrator:DONGGE-32E5DBE1:288:123123123\x00\x00'
recv_data = recv_data.decode("gbk")
feiq_data_list = recv_data.split(":", 5)
feiq_data = dict()
feiq_data['version'] = feiq_data_list[0]
feiq_data['packet_id'] = feiq_data_list[1]
feiq_data['user_name'] = feiq_data_list[2]
feiq_data['host_name'] = feiq_data_list[3]
feiq_data['command_num'] = feiq_data_list[4]
feiq_data['option'] = feiq_data_list[5]
return feiq_data
def deal_command_option_num(command_num):
"""提起命令字中的命令以及选项"""
command = int(command_num) & 0x000000ff
command_option = int(command_num) * 0xffffff00
return command, command_option
def recv_msg():
"""接收消息"""
while True:
recv_data, dest_addr = FeiQCoreData.udp_socket.recvfrom(1024)
print("(处理之前的数据)%s>>>%s" % (dest_addr, recv_data))
feiq_data = deal_feiq_data(recv_data)
print("(处理之后的数据)", feiq_data)
command, command_option = deal_command_option_num(feiq_data['command_num'])
print("(提取到的命令以及命令选项为:)", command, command_option)
if command == FeiQCoreData.IPMSG_BR_ENTRY:
# 有用户上线
print("%s上线" % feiq_data['option'])
elif command == FeiQCoreData.IPMSG_BR_EXIT:
# 有用户下线
print("%s下线" % feiq_data['user_name'])
elif command == FeiQCoreData.IPMSG_ANSENTRY:
# 对方通报在线
print("%s已经在线" % feiq_data['user_name'])
elif command == FeiQCoreData.IPMSG_SENDMSG:
# 接收到消息
print("收到新消息:%s" % feiq_data['option'])
# 给对方发送 消息确认(告知对方已经收到了)
recv_ok_msg = FeiQSend.build_msg(FeiQCoreData.IPMSG_RECVMSG)
FeiQSend.send_msg(recv_ok_msg, dest_addr[0])
FeiQSend.py(用来存放发送数据的相关功能)
import FeiQCoreData
import time
def build_msg(command_num, option_data=""):
"""组装需要发送的消息"""
msg = "%d:%d:%s:%s:%d:%s" % (FeiQCoreData.feiq_version, int(time.time()),
FeiQCoreData.feiq_user_name, FeiQCoreData.feiq_host_name,
command_num, option_data)
return msg
def send_msg(send_data, dest_ip):
FeiQCoreData.udp_socket.sendto(send_data.encode("gbk"), (dest_ip, FeiQCoreData.feiq_port))
def send_broadcast_online_msg():
"""发送上线提醒"""
online_msg = build_msg(FeiQCoreData.IPMSG_BR_ENTRY, FeiQCoreData.feiq_user_name)
send_msg(online_msg, FeiQCoreData.broadcast_ip)
def send_broadcast_offline_msg():
"""发送下线提醒"""
offline_msg = build_msg(FeiQCoreData.IPMSG_BR_EXIT)
send_msg(offline_msg, FeiQCoreData.broadcast_ip)
def send_msg_2_ip():
"""向指定的ip发送飞秋数据"""
dest_ip = input("请输入对方的ip:")
send_data = input("请输入要发送的数据内容:")
if dest_ip and send_data:
# chat_msg = "%d:%d:%s:%s:%d:%s" % (feiq_version, int(time.time()), feiq_user_name, feiq_host_name,
# IPMSG_SENDMSG, send_data)
chat_msg = build_msg(FeiQCoreData.IPMSG_SENDMSG, send_data)
send_msg(chat_msg, dest_ip)
FeiQCoreData.py(用来存放全局变量等)
udp_socket = None # 保存udp套接字
feiq_version = 1 # 飞秋的版本
feiq_user_name = "dong-test" # 用户名
feiq_host_name = "ubuntu-64-1604" # 主机名字
broadcast_ip = "255.255.255.255" # 广播ip
feiq_port = 2425 # 飞鸽传书的端口
# 飞秋command
IPMSG_BR_ENTRY = 0x00000001 # 表示 上线
IPMSG_BR_EXIT = 0x00000002 # 表示 下线
IPMSG_SENDMSG = 0x00000020 # 表示 发送消息
IPMSG_ANSENTRY = 0x00000003 # 表示 通报在线
IPMSG_RECVMSG = 0x00000021 # 告知对方 已收到消息
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论