Python - How To Check an IP Address

Submitted by: 
Visitors have accessed this post 517 times.

In this tutorial we will create a How To Check an IP Address Using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the coding...

Getting started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.

Then copy code that I provided below and paste it inside the IDLE text editor.

  1. import socket
  2. from tkinter import *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.

  1. #=====================================MAIN FRAME======================================
  2. root = Tk()
  3. root.title("Number Guessing Game!")
  4. width = 400
  5. height = 250
  6. screen_width = root.winfo_screenwidth()
  7. screen_height = root.winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.

  1. #=====================================FRAMES==========================================
  2. Top = Frame(root)
  3. Top.pack(side=TOP)
  4. Mid = Frame(root)
  5. Mid.pack(side=TOP, pady=20)
  6. Left = Frame(Mid)
  7. Left.pack(side=LEFT)
  8. Right = Frame(Mid)
  9. Right.pack(side=RIGHT)
  10. Bottom = Frame(root)
  11. Bottom.pack(side=TOP)
  13. #=====================================LABEL WIDGETS=================================
  14. lbl_text = Label(Top, text="Check an IP Address", font=('arial', 20));
  15. lbl_text.pack()
  16. lbl_text2 = Label(Left, text="www.", font=('arial', 16));
  17. lbl_text2.pack()
  18. lbl_result = Label(Bottom, font=('arial', 16));
  19. lbl_result.pack(side=BOTTOM, pady=10)
  21. #=====================================ENTRY WIDGETS=================================
  22. txt_ip = Entry(Right, textvariable=IPADDRESS, font=('arial', 16))
  23. txt_ip.pack()
  26. #=====================================BUTTON WIDGETS================================
  27. btn_check = Button(Bottom, text="Check IP", command=Check_IP)
  28. btn_check.pack()

Creating the Main Function

This is where the code that contains the main functions. This code will get the IP address of the targeted websites when the button is clicked. To do that just copy and write these blocks of code inside the text editor.

  1. #=====================================VARIABLES=======================================
  2. IPADDRESS = StringVar()
  5. #=====================================METHODS=========================================
  7. def Check_IP():
  8. if IPADDRESS.get() == "":
  9. lbl_result.config(fg="Red", text="Please enter a valid website")
  10. else:
  11. lbl_result.config(fg="black", text=socket.gethostbyname("www." + IPADDRESS.get()))

Initializing the Application

After finishing the function save the application as This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.

  1. #=====================================INITIALIZATION==================================
  2. if __name__ == '__main__':
  3. root.mainloop()

There you have it we just created a How To Check an IP Addres Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.