Beginning Network Programming in Python

Posted: April 28, 2012 in Networking, Programming, Python, Socket Programming

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

___________________________________________________________________________________

Happy Coding!

CheerS!!

ΞXΤRΞМΞ-X


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s