[glowpurple] A BEGINNER'S GUIDE TO NETWORK PROGRAMMING IN PYTHON - PART I/III[/glowpurple]

This tutorial introduces the basics of network programming in Python. I'm planning to divide this tutorial into three parts. The first one will cover some basics and details about a few useful functions from the socket module in Python.



ABOUT PYTHON


Python would be the best language for a beginner to start with in the
world of programming. Programming languages tend to obey an inverse complexity
versus simplicity relationship. Take C for instance; the programmer has to worry
about a lot of details of the program which gives him greater control but in turn
makes it more difficult for him to code. For the rest of us who aren't hardcore
programmers trying to play God (Most of us ;-) ), a language more inclined to
simplicity would be the best choice to start learning.


Python is an interpreted, interactive, object-oriented programming language. Best of all,
its open source. It can be dowloaded freely at

http://python.org/download/



"Python - why settle for snake oil when you can have the whole snake?"

—From Usenet posting by Mark Jackson, June 1998



NETWORK PROGRAMMING BASICS


Some of the basic concepts required to understand this tutorial are given below.


IP ADDRESS:

An ip address is a 32-bit address used to identify a computer in the Internet. Therefore
every computer present in the Internet has a unique ip address. This 32 bit is divided
into four equal parts ( 8-bits ) which are separated by dots. Each address consists of a network number,
an optional subnetwork number, and a host number.

For eg. 165.113.245.2

Each part having just 8-bits can have values ranging from 0 to 255 ( 2^8 =256 values ). But, 0 and
255 are used for special purposes and are not used to specify a particular box.


PORTS:

"A Port is a connection point for different protocols to communicate on different machines"
For eg. port number 80 is used for HTTP traffic. Try using the command "netstat -a" while
surfing the web to see the various ports involved in the activity. To get a complete listing
of well known ports, check this site out -> http://www.iana.org/assignments/port-numbers


SOCKETS:

"Sockets are used as endpoints for sending and receiving data between computers."
When i'm using my browser to access www.antionline.com, my browser( good ol' firefox)
creates a socket requesting connection to the site. At the same time, the antionline
webserver has a socket listening for connections. In this way sockets are used to send
data between the two boxes. A socket is bound to an ip-address and a port.


TRANSPORT LAYER:

The transport layer is the fourth layer of the seven layer OSI-ISO model. The transport layer
provides for transport of messages between boxes. The two protocols working in this layer are
TCP ( Transmission Control Protocol ) and UDP ( User Datagram Protocol ). The former provides
a very reliable form of communication. TCP employs a technique called a three way handshake.
UDP on the other hand is known as a connectionless protocol and is very unreliable. I'm only
talking about the transport layer of the OSI-ISO model as it is necessary to understand the
difference between UDP and TCP while creating a socket. For more on the OSI-ISO model, check
out the site -> http://www.pcsupportadvisor.com/OSI_...odel_page1.htm





NETWORK PROGRAMMING IN PYTHON





The "sockets" module in Python is quite similar to that in C. C network programmers will find it easy
as cake to master python network programming. Throughout the tutorial, i'll use ">>>" to indicate the
CLI of the Python interactive interpreter. The statement followed by "#" is a comment and is not
interpreted.


HOST NAMES & IP ADDRESSES


* gethostname()

The name of the function explains the purpose ;-)


-------------------------------------------------------------------------------------------------------------------------

>>>import socket # Import the various functions used here from the "socket" module
>>>socket.gethostname() # Call the function
'aria' # The hostname of my box

-------------------------------------------------------------------------------------------------------------------------


* gethostbyname(name)

This function returns the ipaddress of the hostname specified.



-------------------------------------------------------------------------------------------------------------------------

# Not including the import command to avoid repetition

>>>socket.gethostbyname('www.antionline.com) # Call the function
'63.146.109.212' # The Ip address ... cool huh ??

-------------------------------------------------------------------------------------------------------------------------




* gethostbyaddr(ipaddress)

This function returns a tuple returning the primary hostname, a list of alternate hostnames
and a list of other Ip addresses for the same domain.





-------------------------------------------------------------------------------------------------------------------------

>>> socket.gethostbyaddr('127.0.0.1') # this is the self referential ip of ur box
('localhost', [], ['127.0.0.1'])

-------------------------------------------------------------------------------------------------------------------------



*getservbyname(service,protocol)


This is used for obtaining the port number corresponding to a given service (http,udp etc.) and
protocol( udp or tcp )



-------------------------------------------------------------------------------------------------------------------------

>>>socket.getservbyname('http','tcp')
80
>>>socket.getservbyname('doom','tcp')
666 # Hail satan ! .. ;-) ..jus kidding
>>> socket.getservbyname('http','udp')

Traceback (most recent call last):
File "<pyshell#4>", line 1, in -toplevel-
socket.getservbyname('http','udp')
error: service/proto not found # This explains why browsing the web is so reliable ;-}

-------------------------------------------------------------------------------------------------------------------------





* inet_aton(ip address)

Used to convert ip addresses into their packed 32-bit form used by programs.





-------------------------------------------------------------------------------------------------------------------------

>>> socket.inet_aton('127.0.0.1')
'\x7f\x00\x00\x01'

-------------------------------------------------------------------------------------------------------------------------



inet_ntoa(packedform) is used to convert it back to the ip address form, the details of which
are self explanatory...( feeling lazy !)








This brings an end to the first part of this tutorial. The next part will deal with socket creation,
deletion and sending data between boxes. Thank you for spending your time to read my little tutorial.







REFERENCES

1. www.python.org
2. Python Bible
3. Google .. the Oracle