Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: get_ip.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This script will return the local and public IP addresses of the machine.
- Requirements:
- - Python 3.x
- - requests library
- Functions:
- - line: Draws a separation line in the console output.
- - get_local_IP: Retrieves and prints the local IP address from socket.
- - get_public_IP: Retrieves and prints the public IP address from an external service.
- Usage:
- - Run the script using the python command:
- 'python get_ip.py'
- Additional Notes:
- - Ensure you have an active internet connection for retrieving the public IP address.
- """
- import socket
- import requests
- # Function to draw a separation line
- def line():
- print("-" * 40)
- line()
- print("Gathering Local & Public IP Addresses...")
- line()
- # Function to get Local IP
- def get_local_IP():
- try:
- host_name = socket.gethostname()
- local_ip = socket.gethostbyname(host_name)
- print("\nLocal IP:\n ", local_ip)
- except socket.gaierror as e:
- print("\nUnable to get Local IP! Socket error occurred.\nError:", e)
- except Exception as e:
- print("\nUnable to get Local IP!\nError:", e)
- # Function to get Public IP from http://www.icanhazip.com
- def get_public_IP():
- try:
- response = requests.get("http://www.icanhazip.com")
- response.raise_for_status() # Check if the request was successful
- public_ip = response.text.strip()
- print("\nPublic IP:\n ", public_ip)
- line()
- except requests.RequestException as e:
- print("\nUnable to get Public IP! Request error occurred.\nError:", e)
- except Exception as e:
- print("\nUnable to get Public IP!\nError:", e)
- # Return results
- print("RESULTS:")
- line()
- get_local_IP()
- get_public_IP()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement