在使用Python的服务器上是否找到了用于测试给定页面的应用程序
先决条件: Python Urllib 模块
在本文中,我们将编写脚本以使用 GUI 应用程序测试是否在服务器上找到给定页面。我们需要安装 Urllib 模块来执行这个操作。在终端中键入此命令。
pip install urllib
方法:
- 导入 urllib 模块。
- 使用 urllib.request.urlopen() 读取 URL。
- 检查读取 URL 是否给出任何异常。
执行:
Python3
# import module
from urllib.request import urlopen, URLError, HTTPError
# exception handling to
# catch URL error
try:
html = urlopen("https://www.geeksforgeeks.org/")
except URLError as e:
print("Server not found!")
except HTTPError as e:
print("HTTP error")
else:
print("Server found")
Python3
# import modules
from tkinter import *
from urllib.request import urlopen, URLError
# user defined function
def URL_check():
try:
html = urlopen(str(e1.get()))
except URLError as e:
res = "Server not found!"
else:
res = "Server found"
result.set(res)
# object of tkinter
# and background set to light grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
result = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Enter URL : ", bg="light grey").grid(row=1, sticky=W)
Label(master, text="Status :", bg="light grey").grid(row=3, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result,
bg="light grey").grid(row=3, column=1, sticky=W)
e1 = Entry(master, width=50)
e1.grid(row=1, column=1)
# creating a button using the widget
b = Button(master, text="Check", command=URL_check, bg="white")
b.grid(row=1, column=2, columnspan=2, rowspan=2, padx=5, pady=5,)
mainloop()
输出:
Server found
在带有Tkinter的服务器上是否找到了用于测试给定页面的应用程序:此脚本将上述实现与 GUI 相结合。
蟒蛇3
# import modules
from tkinter import *
from urllib.request import urlopen, URLError
# user defined function
def URL_check():
try:
html = urlopen(str(e1.get()))
except URLError as e:
res = "Server not found!"
else:
res = "Server found"
result.set(res)
# object of tkinter
# and background set to light grey
master = Tk()
master.configure(bg='light grey')
# Variable Classes in tkinter
result = StringVar()
# Creating label for each information
# name using widget Label
Label(master, text="Enter URL : ", bg="light grey").grid(row=1, sticky=W)
Label(master, text="Status :", bg="light grey").grid(row=3, sticky=W)
# Creating lebel for class variable
# name using widget Entry
Label(master, text="", textvariable=result,
bg="light grey").grid(row=3, column=1, sticky=W)
e1 = Entry(master, width=50)
e1.grid(row=1, column=1)
# creating a button using the widget
b = Button(master, text="Check", command=URL_check, bg="white")
b.grid(row=1, column=2, columnspan=2, rowspan=2, padx=5, pady=5,)
mainloop()
输出:
检查另一个 URL。