📜  创建一个 GUI 来使用Python查找域名的 IP

📅  最后修改于: 2022-05-13 01:55:05.199000             🧑  作者: Mango

创建一个 GUI 来使用Python查找域名的 IP

先决条件: Python GUI – tkinter

在本文中,我们将看到如何从域名中查找 IP 并使用Python将其与 GUI 应用程序绑定。我们将使用iplookup模块从域名中查找 IP。它是一个小模块,它接受单个域作为字符串,或多个域作为列表,并返回关联 IP 的列表。

将此代码运行到您的终端进行安装。

pip install iplookup

方法:

  • 导入模块
  • 创建 iplookup 的对象
  • 将域传递给 iplookup obj
  • 现在遍历IP

执行:

Python3
# import module
from iplookup import iplookup
  
#create object of iplookup
ip = iplookup.iplookup
  
# Input by geek
# domain name
domain = "geeksforgeeks.org"
  
# pass the domain
# into iplookup obj
result = ip(domain)
  
# traverse the ip
print("Domain name : ",domain)
print("Ip : ",result)


Python3
# import modules
from tkinter import *
from tkinter import messagebox
from iplookup import iplookup
  
  
def get_ip():
    try:
        ip = iplookup.iplookup
        result = ip(e.get())
        res.set(result)
  
    except:
        messagebox.showerror("showerror", "Something wrong")
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
res = StringVar()
  
# Creating label for each information
# name using widget Label
Label(master, text="Enter website name :",
      bg="light grey").grid(row=0, sticky=W)
Label(master, text="Result :", bg="light grey").grid(row=1, sticky=W)
  
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
    row=1, column=1, sticky=W)
  
  
e = Entry(master)
e.grid(row=0, column=1)
  
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=get_ip)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
  
mainloop()


输出:

Domain name :  geeksforgeeks.org
Ip :  ['34.218.62.116']

使用 Tkinter 从域 GUI 应用程序中查找 IP:此脚本将上述实现实现到 GUI 中。

蟒蛇3

# import modules
from tkinter import *
from tkinter import messagebox
from iplookup import iplookup
  
  
def get_ip():
    try:
        ip = iplookup.iplookup
        result = ip(e.get())
        res.set(result)
  
    except:
        messagebox.showerror("showerror", "Something wrong")
  
  
# object of tkinter
# and background set for light grey
master = Tk()
master.configure(bg='light grey')
  
# Variable Classes in tkinter
res = StringVar()
  
# Creating label for each information
# name using widget Label
Label(master, text="Enter website name :",
      bg="light grey").grid(row=0, sticky=W)
Label(master, text="Result :", bg="light grey").grid(row=1, sticky=W)
  
  
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=res, bg="light grey").grid(
    row=1, column=1, sticky=W)
  
  
e = Entry(master)
e.grid(row=0, column=1)
  
# creating a button using the widget
# Button that will call the submit function
b = Button(master, text="Show", command=get_ip)
b.grid(row=0, column=2, columnspan=2, rowspan=2, padx=5, pady=5)
  
mainloop()

输出: