Hey everyone, I would like to show you some basic network programming stuff in python (v2 or higher). This is purely for coders who have some basic knowledge in python and want to begin network programming in Python.
So, open up your Python Interpreter or Python IDE and open a new window. I am gonna show you some basic stuff you can do like how to get your host name or I.P. address using python. Of course, there is a much shorter way to write this code. I have written this code for beginners to understand what is actually going on.
# Program to print your host name import socket # This imports the socket module for using its functions hostname=socket.gethostname() # This will return your hostname print "Your Host name is : " + name raw_input("Enter any key to continue...") #End of Code
Save it and run it. It will print your hostname. Simple rite?? Yeah! There is your first simple program.
Now, let’s write another simple program used to resolve a given hostname to its I.P. Address. This is done by using the function socketname.gethostbyname(hostame)
# Hostname Resolver import socket hostname=raw_input("Enter the hostname to resolve :") ip=socket.gethostbyname(hostname) # Return corresponding IP address to variable ip print "IP ADDRESS : " + ip # Prints the retrieved IP Address raw_input("Enter any key to continue.. ") # End of Code