Results 1 to 3 of 3

Thread: Beginning Network Programming in Python

  1. #1
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004

    Lightbulb Beginning Network Programming in Python


    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.


    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

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

    —From Usenet posting by Mark Jackson, June 1998


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


    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.

    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.


    "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 ->


    "Sockets are used as endpoints for sending and receiving data between computers."
    When i'm using my browser to access, 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.


    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 ->


    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


    * 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(' # Call the function
    '' # 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('') # this is the self referential ip of ur box
    ('localhost', [], [''])



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


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

    Traceback (most recent call last):
    File "<pyshell#4>", line 1, in -toplevel-
    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('')


    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.


    2. Python Bible
    3. Google .. the Oracle

  2. #2
    In And Above Man Black Cluster's Avatar
    Join Date
    Feb 2005
    Hi Packet,

    The tutorial is underdone, what you have provided yet, are merely simple definitions ... I would highly recommend a Re-done to the tutorial and more effort would boosts the professionalism of the tutorial in general ....

    \"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards - and even then I have my doubts\".....Spaf
    Everytime I learn a new thing, I discover how ignorant I am.- ... Black Cluster

  3. #3
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Thanks for the reply Black Cluster. I intended to keep the first part very fundamental and to cover the more programming aspects in the coming parts. Anyway i shall make more changes
    to increase the depth and professionalism. :-)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts