Python Network programming – Part 1

Lets get into the Python network Automation as it is an interpreted, object-oriented, high-level programming language with dynamic semantics.

Network programming is a major use of Python. Python standard library has wide support for network protocols, data encoding/decoding, and other things you need to make it work.

Writing network programs in Python tends to be substantially easier than in C/C++

Why use Python?

Python also has some advantages over other scripting languages. It has a simple syntax and is conceptually clear, making it easy to learn. Python is also easier and more descriptive when using complex data structures (such as lists, dictionaries, and tuples). Python can also extend languages and be extended by languages in turn.

I find that Python’s syntax makes it more readable and maintainable than Perl but less so than Ruby. Python’s advantage over Ruby is the large number of libraries and modules that are available. Using these, you can build feature-rich programs with little custom code.

Python’s use of indentation to identify block scope can be rather annoying, but its simplicity tends to make up for this minor flaw.

Simple Socket:

Sockets are used to connect two or more computers together and interchange commands/instructions between them.

One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. Server forms the listener socket while client reaches out to the server.

They are the real backbones behind web browsing. In simpler terms there is a server and a client.

Socket programming is started by importing the socket library and making a simple socket.

Making a server :

First of all let me explain a little bit about a server. A server has a bind() method which binds it to a specific ip and port so that it can listen to incoming requests on that ip and port. Next a server has a listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And lastly a server has an accept() and close() method. The accept method initiates a connection with the client and the close method closes the connection with the client. Now lets begin making our simple server:

Making a client :

Now we need something with which a server can interact. We could telnet to the server like this just to know that our server is working.



  1. The name for a “Thing”
  2. An object is created from a class


The blueprint for an object; declaring what the thing has and what the thing does


What the thing has


What the thing does; There are no different from subroutines

In our setup,  version of phython Ubantu = 2.7.12

Show file = cat shashitelnet
edit file = nano shashitelnet
Execute file = Python shashitelnet
Enter in python = python
Exit from Python = exit ()

Control + x n then save and give file name:

  1. For telnet and creating loopback,


Similarly we can add more loopback or ospf/other protocol just like below:



** Copy python file from previous script  = cp shashitelnet shashivlan





Note: chmod +x ./shashivlan     <<<< giving permission to the file, for execution

Execute file now. ==> ./shashivlan

Ls  == > shows the number of files.

—–now, running sript to run the configuration in the devices and save the config in your system from all devices,


I have mentioned #comment for description understanding in the script 

 LOOP ==> You can avoid creating multiple vlan or instances…directly create loop range for no. items to be created, like below:


In this above, used range (5,19 ) ==> which has created 5 to 18 vlans ..always 1 number less —> with a vlan description to name “Python_VLAN_”


##### SUB-LOOP, for configuring multiples switches at same time with same information:



*** This will configure — switches, starting from 192.168.122.(72) to (77)…All with same username and password.

It’ll configure the range of VLAN in all the switches at same time.

 Another addition in the srcipt for  multiple devices is saving the configuration after making the changes in the config.

In the same config, add “wr” to save the config in all devices



#### Creating LOOP, when u want to make changes/config in selected devices…which are not in sequence:


  1. Create a file with IP address of the devies: 

# nano Myswitches

2. Create another file “testloop” which defines action on the Mydevies file, like below:


Now, creating the main srcipt where, we will call the above prepared scripts::


This will login into devices and make the changes..

Here is the summary of the key functions from: 

  1. socket(): Create a new socket using the given address family, socket type and protocol number.
  2. bind(address): Bind the socket to address.
  3. listen(backlog): Listen for connections made to the socket. The backlogargument specifies the maximum number of queued connections and should be at least 0; the maximum value is system-dependent (usually 5), the minimum value is forced to 0.
  4. accept(): The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection.
  5. At accept(), a new socket is created that is distinct from the named socket. This new socket is used solely for communication with this particular client.
  6. For TCP servers, the socket object used to receive connections is not the same socket used to perform subsequent communication with the client. In particular, the accept()system call returns a new socket object that’s actually used for the connection. This allows a server to manage connections from a large number of clients simultaneously.
  7. send(bytes[, flags]): Send data to the socket. The socket must be connected to a remote socket. Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data.
  8. colse(): Mark the socket closed. all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly.

Note that the server socket doesn’t receive any data. It just produces client sockets. Each client socket is created in response to some other client socket doing a connect() to the host and port we’re bound to. As soon as we’ve created that clientsocket, we go back to listening for more connections.

In a later post, I will show you how to write the skill.

Leave me a comment of how you think the Skill can be improved.


Categories: Programming, Python

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s