#!/usr/bin/python3
import socket
import tkinter as tk
def check_ip_port(ip, port):
print(ip)
print(port)
root1 = tk.Tk()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(4) # 超时为4秒
try:
s.connect((ip, int(port)))
msg = s.recv(64)
# s.connect(('10.54.2.134', 22))
# 接收小于 1024 字节的数据
except TimeoutError as err:
info_label0 = tk.Label(root1, text='ip端口不通')
except Exception as err:
info_label0 = tk.Label(root1, text='ip端口不通')
else:
info_label0 = tk.Label(root1, text='ip端口是通的')
finally:
# info_label0.grid(column=0, row=1, padx=2, pady=2)
info_label0.pack()
btn1 = tk.Button(root1, command=root1.destroy)
# btn1.grid(column=0, row=2, padx=2, pady=2)
btn1.pack()
btn1["text"] = "确定"
root1.title('测试连通性')
root1.geometry("245x60")
s.close()
def ip_port_connection():
ip = ip_entry.get()
port = prot_entry.get()
#root.destroy()
check_ip_port(ip, port)
root = tk.Tk()
# 创建第一个输入框
ip_label = tk.Label(root, text="ip:")
ip_label.grid(column=0,row=0,padx=2,pady=2)
ip_entry = tk.Entry(root,bd=1,relief=tk.SOLID)
ip_entry.grid(column=1,row=0,columnspan=2,padx=2,pady=2)
# 创建第二个输入框
prot_label = tk.Label(root, text="端口:")
prot_label.grid(column=0,row=1,padx=2,pady=2)
prot_entry = tk.Entry(root,bd=1,relief=tk.SOLID)
prot_entry.grid(column=1,row=1,columnspan=2,padx=2,pady=2)
# 创建一个按钮,并绑定事件处理函数
button = tk.Button(root, text="确定", command=ip_port_connection)
button.grid(column=1,row=2,columnspan=3,padx=2,pady=2)
root.title('测试连通性')
root.geometry("250x100") #不能设宽度,才能居中
# 运行窗口的主循环
root.mainloop()