飞鸽传书-上下线-命令版
from socket import *
import time
import os
udp_socket = None
feiQ_port = 2425
feiQ_version = 1
feiQ_user_name = "dongge-test"
feiQ_host_name = "mac-pro"
broadcast_ip = "255.255.255.255"
IPMSG_BR_ENTRY = 0x00000001
IPMSG_BR_EXIT = 0x00000002
def create_udp_socket():
"""创建udp套接字"""
global udp_socket
udp_socket = socket(AF_INET, SOCK_DGRAM)
udp_socket.bind(("", feiQ_port))
udp_socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
def send_broadcast_online():
"""发送上线广播"""
msg = "%d:%d:%s:%s:%d:%s" % (feiQ_version, int(time.time()), feiQ_user_name, feiQ_host_name, IPMSG_BR_ENTRY, feiQ_user_name)
udp_socket.sendto(msg.encode('gbk'), (broadcast_ip, feiQ_port))
def send_broadcast_offline():
"""发送下线广播"""
msg = "%d:%d:%s:%s:%d:%s" % (feiQ_version, int(time.time()), feiQ_user_name, feiQ_host_name, IPMSG_BR_EXIT, feiQ_user_name)
udp_socket.sendto(msg.encode('gbk'), (broadcast_ip, feiQ_port))
def command_menu():
"""打印命令功能菜单"""
os.system("clear")
print(" 飞鸽传书(python)-v1.0")
print(" 1: 发送上线广播")
print(" 2: 发送下线广播")
print(" 0: 退出飞秋")
print("")
command_str = input("请输入数字:")
return command_str
def main():
"""完成整体控制"""
create_udp_socket()
while True:
command_str = command_menu()
if command_str == "1":
send_broadcast_online()
elif command_str == "2":
send_broadcast_offline()
elif command_str == "0":
send_broadcast_offline()
exit()
if __name__ == '__main__':
main()