一、效果展示
二、代码实现
import nmap
import socket
import tkinter as tk
from tkinter import messagebox,Listbox
from threading import Thread#获取自身IP,从而确定当前局域网范围
def get_ip_address():#创建了一个socket对象,socket.AF_INET表示使用IPv4地址族,socket.SOCK_DGRAM表示使用UDP#使用UDP是因为不需要真正发送数据,通过连接一个地址来促使系统分配一个端口s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)try:# 不需要真正发送数据,所以使用一个不存在的地址#这段代码目的让操作系统分配一个本地端口,并返回当前机器IPs.connect(('255.255.255.255', 1))ip = s.getsockname()[0]print(s.getsockname())#print(type(s.getsockname()))except Exception as e:print(e)finally:s.close()# 找到第三个'.'的位置dot_index = ip.rfind('.')# print(dot_index)# 替换第三个'.'及其后的内容new_ip = ip[:dot_index] + '.0/24'return new_ipdef scan_local_network(listbox):nm = nmap.PortScanner()try:# 执行主机发现扫描network = get_ip_address()nm.scan(hosts=network, arguments='-sn')#清空列表框listbox.delete(0,tk.END)# 打印扫描结果for host in nm.all_hosts():status = nm[host].state()if status == 'up':listbox.insert(tk.END,host)except nmap.PortScannerError as e:messagebox.showerror("扫描失败",f"An error occurred:{e}")# 发送TCP消息的函数
def send_message():ip = ip_entry.get()port = int(port_entry.get())message = message_entry.get()try:with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:sock.connect((ip, port))sock.sendall(message.encode())messagebox.showinfo("发送成功", "消息已发送到" + ip)except Exception as e:messagebox.showerror("发送失败", str(e))# 更新IP输入框的函数
def update_ip(ip):ip_entry.delete(0, tk.END)ip_entry.insert(0, ip)# 定义启动TCP服务器的函数
def start_server(port):global server_sockettry:server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)server_socket.bind(('0.0.0.0', port)) # 监听所有接口上的端口server_socket.listen(1)messagebox.showinfo("服务器启动", f"服务器正在监听端口 {port}")while True:client_socket, addr = server_socket.accept()with client_socket:while True:data = client_socket.recv(1024)if not data:breakmessage_text.insert(tk.END, f"从{addr}接收到消息: {data.decode()}\n")client_socket.close()except Exception as e:messagebox.showerror("接收消息失败", str(e))finally:if 'server_socket' in globals() and server_socket:server_socket.close()# 在GUI中启动服务器的按钮
def start_server_button_clicked():port = int(port_entry.get()) # 获取用户输入的端口server_thread = Thread(target=start_server, args=(port,))server_thread.start() # 启动线程# 初始化GUI界面
root = tk.Tk()
root.title("TCP消息发送器")# 使用grid布局管理器
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=2)# IP输入框
ip_label = tk.Label(root, text="IP:")
ip_label.grid(row=0, column=0, padx=10, pady=5, sticky='e')
ip_entry = tk.Entry(root, width=20)
ip_entry.grid(row=0, column=1, padx=10, pady=5)# 端口输入框
port_label = tk.Label(root, text="PORT:")
port_label.grid(row=1, column=0, padx=10, pady=5, sticky='e')
port_entry = tk.Entry(root, width=20)
port_entry.grid(row=1, column=1, padx=10, pady=5)# 消息输入框
message_label = tk.Label(root, text="Message:")
message_label.grid(row=2, column=0, padx=10, pady=5, sticky='e')
message_entry = tk.Entry(root, width=50)
message_entry.grid(row=2, column=1, padx=10, pady=5)# 扫描按钮
scan_button = tk.Button(root, text="扫描局域网", command=lambda: scan_local_network(results_listbox))
scan_button.grid(row=3, column=0, padx=10, pady=5, sticky='w')# 发送按钮
send_button = tk.Button(root, text="发送", command=send_message)
send_button.grid(row=3, column=1, padx=10, pady=5, sticky='w')# 启动服务器按钮
start_server_button = tk.Button(root, text="启动服务器", command=start_server_button_clicked)
start_server_button.grid(row=4, column=1, padx=10, pady=5, sticky='w')# 显示扫描结果的列表框
results_listbox = tk.Listbox(root, width=35, height=10)
results_listbox.grid(row=5, column=0, columnspan=2, padx=10, pady=5, sticky='nsew')# 消息显示区域
message_text = tk.Text(root, width=50, height=10, wrap='word')
message_text.grid(row=6, column=0, columnspan=2, padx=10, pady=5, sticky='nsew')# 监听端口标签和输入框(放置在Frame中以避免布局冲突)
frame = tk.Frame(root)
frame.grid(row=7, column=0, columnspan=2, padx=10, pady=5, sticky='ew')listen_port_label = tk.Label(frame, text="Listening Port:")
listen_port_label.pack(side=tk.LEFT, padx=10, pady=5)
listen_port_entry = tk.Entry(frame, width=20)
listen_port_entry.pack(side=tk.LEFT, padx=10, pady=5)# 为扫描结果列表框添加选择事件
#event参数是tkinter事件对象,包含了事件相关信息
def on_select(event):#通过事件对象的widget属性获取触发事件的组件,即被点击的列表框w = event.widget#调用列表框w的curselection()方法,获取当前被选中的项目索引列表,如果没有项目被选中,则返回空列表selection = w.curselection()if selection:#事实上curselection返回的只有一个值index = selection[0]#获取IPip = w.get(index)#更新IPupdate_ip(ip)#将on_select函数绑定到results_listbox列表框的<<ListboxSelect>>事件上,意味着每当列表框中的项目被选中时,
#就会触发on_select函数
results_listbox.bind('<<ListboxSelect>>', on_select)# 启动GUI主循环
root.mainloop()
三、注意事项
<1>想要实现扫描局域网功能,主机必须安装有nmap
<2>在发送消息前,接收端需要监听对应端口如上述展示中的1234,之后发送端可以向接收端监听的端口发送信息
<3>在手机热点构成的局域网下,某些电脑将无法实现发送信息功能,即使对方已经监听对应端口(至少我的电脑就不可以)