Python Forum
IP address for users - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: IP address for users (/thread-40876.html)



IP address for users - Robots - Oct-07-2023

Hi all -

Using Python 3 .

I have been tasked with logging the IP address, date and time when users attempt to access a database. There are many ways to find MY address, but so far I can't figure out how to capture someone else's IP.

Using this code now -

from datetime import datetime

# Importing socket library
import socket

attempts = 0
 
# Get the current date and time
current_datetime = datetime.now()

# Update the number of attempts
attempts += 1

print(current_datetime)
print(attempts)

# Function to display hostname and IP address
def get_Host_name_IP():


    try:
        host_name = socket.gethostname()
        host_ip = socket.gethostbyname(host_name)
        print("Hostname :  ", host_name)
        print("IP : ", host_ip)
        
    except:
        print("Unable to get Hostname and IP")
 
 
# Driver code
get_Host_name_IP()  # Function call
But I am not sure if this is just capturing my info or if it will capture attempts to login to the database.
...and yes, I realize the datetime and attempts are simply printing at this point.