Thursday, July 30, 2015

Python - Exception Handling

As in all High level languages, Python also provides ways for handling Issues.  Python provides 2 features to handle any unexpected error in the Python programs.

Exception Handling

Exception Handling – An Exception is an event that occurs during an execution of program that disrupts the flow of the program. Normally when a Python program encounters with a situation that it cannot handle, it raises and exception.

These generated exceptions needs to be handled correctly or the program will terminate.

Try – Except – Python programs are written inside a try-except block so that if there is any code that might cause a issues, it will be handled. The program is written inside a try block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible.

Here is the Exception Handling syntax,

   You do your operations here;
except ExceptionI:
   If there is ExceptionI, then execute this block.
except ExceptionII:
   If there is ExceptionII, then execute this block.
   If there is no exception then execute this block.

Some important points to remember are,
1) a single try statement can have multiple except Statements so that  when a try block contains statements that may throw a different type of exceptions.

2) We can have our own generic except clause, which can handle exceptions.

3) The else clause in the except statement above is executed only when there are no exceptions raised

AN Example for exception handling would look like this,
>>> try:
...     fh = open("Master.txt","w")
...     print
... except IOError:
...     print "Error Cannot Find File"
... else:
...     print "File Obtained"
File Obtained

Except Statement with no Exceptions – In the above snippet, if we observe we have an IOError defined in the except clause. We can also write except clause with no exceptions defined. This is considered a bad programming practice though it catches all the exceptions but will not let the programmer know what exactly the Exceptions is.

You can also use the same except statement to handle multiple exceptions like,

   You do your operations here;
except(Exception1[, Exception2[,...ExceptionN]]]):
   If there is any exception from the given exception list,
   then execute this block.
   If there is no exception then execute this block.

Finally Clause - An exception allows one to handle any exceptions raised but there will be some cases where though an exception raised, a piece of code needs to be executed. Such sort of code is written in Finally. Consider when file is opened in read mode and tried to write to it , then an exception is thrown and handled but at-last the opened file needs to be closed. In this cases we write the closing of file in the finally block. An syntax would look like this,

   Operations Here
   Due to any exception, this may be skipped.
   This would always be executed.

Exception Arguments – An Exception in Python can have an argument which gives additional information about the problem or the exception caught. The syntax will be like,

>>> def ret_value(var):
...     try:
...             return int(var)
...     except ValueError,Argument:
...             print "Argument Passed",Argument
>>> ret_value("hello WOrld")
Argument Passed invalid literal for int(): hello World

The variable receives the value of the exception mostly containing the cause of the exception. The variable can receive a single value or multiple values in the form of a tuple. This tuple usually contains the error string, the error number, and an error location.

Raising an Exception – An Exception can be raised when certain Exceptions are caught. Consider we have our own custom exceptions raised when specific conditions met. Python allows us to raise exceptions like,

raise [Exception [, args [, traceback]]]

An Example of Raising an Exception will be,

>>> def fun_name(var):
...     if(var < 1):
...             raise "Invalid Var",var
>>> fun_name(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in fun_name

Invalid Var: 0
Read More

Sunday, July 26, 2015

Python - Modules

A Module or a Package allows you to logically arrange or organize your code. This allows us to group related code into a module which makes the code easier to understand and code.

A module in a Python is an Python object which can be used to bind and reference.

A Module in a Python consists of a Python code. A module can define functions, classes, variables along with a run able code.

Now lets see a basic Example on how to use Python Modules. Write a below sample python Function in a file and save that as

def printFun(str):
    print "Hello , This is Passed String " , str

Once the file is saved open the Python interpreter and invoke the commands as,

>>> import support
>>> support.printFun("Python")
Hello, This is Passed String Python

We can see after importing the module support, we can directly call methods implements inside the support python code.

Import -  We can use any python source file as a module by executing an import statement in some other python code using the import statement. The syntax looks as

import module1[, module2[,... moduleN]

Normally when the python encounters with the import statement, the interpreter imports the module as it is present in the search path. The Search path is a list of directories that the interpreter searches before importing a module.

Python does contain some default Environment variables for searching for the modules

PYTHONHOME - Location of standard libraries
PYTHONPATH - Additions to standard search path for modules
PYTHONSTARTUP - Interactive mode; commands run before first input is prompted FOR

From – A from Statement allows us to use a specific attributes from a module into the current name space or code. The syntax looks as

from modname import name1[, name2[, ... nameN]]

The above example can be re-written as

>>> from support import printFun
>>> printFun("Python")
Hello , This is Passed String  Python

All modules can be imported using - From support import *

Locating Modules
When a module is imported, python interpreter searches for the modules in the following way,

1) First it searches in the Current directory
2) If not available, it goes and searches in the location set by PYTHONPATH
3) If the module is not available even in the PYTHONPATH, it then searches the location

The location search path is set in the module sys. We can get this using sys.pathvariables. The sys.path variables contains the current directory and installation path

The PYTHONPATH Variable - The PYTHONPATH is an environment variable, consisting of a list of directories.
Read More

Python – Basic Syntax

Python can be used as both interactive or script mode. 

Interactive Mode – The Interactive mode can be used by starting the interpreter  provided by Python. This can be done by executing the “python” command in the Shell as, 

Executing your first “Hello World “ Program with the Interactive mode is Quite Simple. Enter the below at the command mode provided ( after the >>> )

Print “hello World” 

The Output the command Is displayed there it-self allowing developers to test and debug their snippets of code. To come out of the Interpreter , we can use “CTRL + D” .

Script Mode - Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Open a file using the VI Editor and enter the below statement and Save as (Python files have

Print “Hello World”

Now invoke the interpreter using the save file as

:) [xprk477@vx181d /tmp]$ python /tmp/
Hello World

Read More

Friday, July 24, 2015

Python - File Management

A File is a chunk of logically related data or information which can be used by computer programs.

Generally every Programming language provides the ability to store and retrieve information from file. The most basic tasks involving files will be reading and writing data.

Python also provides facilities for manipulating files. The logic for file management is provided directly without any additional functions or modules needed ( unlike in other programming language where additional modules are imported to work with files)

The syntax for reading and writing files in Python is similar to programming languages like C and C++ or Perl, but easier to handle. 

Open - In order to perform basic operations on files we need to first open it. We use the Open() function for opening the files after which creates  a file Object that is used to call other support methods associated with that.

The syntax is –
File Object = open(“File Name”, access mode, buffering)

File Name – Name argument is a String value that contains the name of the file being opened.

Access Mode – The access mode tells the mode in which is file is opened. File modes include read, write, append etc. The access mode is optional and the default one is read

Buffering – Buffering of the file contents is disabled if that is set to 0 and enabled when used 1. When a value more than 1 is used, buffering takes place with that value.
If a negative value is defined the default is used. This is an options value

The basic file modes are

R        – Open file for read only.
Rb      – open file for reading in binary more.
R+      - open file for both reading and writing
Rb+    - open files for reading and writing in binary mode
W       – Open file in write mode
W+     - Open file in read and write mode
Wb     – write in binary mode
A        - Append
A+      - Both  for appending and reading

File Objects - Now once the file is opened, the file object does have attributes that can be used to get information regarding the file. The details include

file.closed      Returns true if file is closed, false otherwise.
file.mode       Returns access mode with which file was opened.       Returns name of the file.

An Example – Now lets see an Example of Opening a File,

>>> fo =open("hunter.txt","wb")
>>> print
>>> print fo.closed
>>> print fo.mode

Writing to File -
>>> fo = open("hunter.txt","wb")
>>> fo.write("this is Python")
>>> fo.close()

[djas999@vx181d imp]$ cat hunter.txt
this is Python

Reading from a File -
>>> fo = open("hunter.txt","r+")
'this is Py'

Now the above read and write methods do the same thing as the name says. The read() method takes an integer value as argument which is the number of bytes to read. This starts from file and read bytes specified in file.

Closing – a method close() can be used to close the file

File Positions - When Ever we start reading a file , the file pointer starts from first and then moves on. Some times we may need to find out the pointer location or want to move the pointer to a specific location.

Tell() – The tell() method tells you the current position within the file.
Seek(offset, from) – This method changes the current file position. The offset indicates the number of bytes to be moved.  The from argument specifies the reference position from where the bytes are to be moved.

If from is set to 0, it means use the beginning of the file as the reference position and 1 means use the current position as the reference position and if it is set to 2 then the end of the file would be taken as the reference position.

Some more examples include,

To read one line at a time, use:
fh = open("hello".txt", "r")
print fh.readline()

To read a list of lines use:
fh = open("hello.txt.", "r")
print fh.readlines()

To append to file, use:
fh = open("Hello.txt", "a")
write("Hello World again")

To close a file, use
fh = open("hello.txt", "r")

Read More

Monday, July 20, 2015

Python- Set

Set – A Set in Python is a Un-ordered Collection of Unique and immutable Objects. The Set does not allow multiple occurrences of the same element.

A Set is created using the set() function available as

>>> x = set("A Python Tutorial")
>>> print x
set(['A', ' ', 'i', 'h', 'l', 'o', 'n', 'P', 'r', 'u', 't', 'a', 'y', 'T'])
>>> type(x)
<type 'set'>

We can pass a list or Tuple to the Set as - x = set(["Perl", "Python", "Java"])

A tuple can be sent to Set as
>>> cities = set(("Paris", "Lyon", "London","Berlin","Paris","Birmingham"))
>>> print cities
set(['Paris', 'Birmingham', 'Lyon', 'London', 'Berlin'])

But if we print the Tuple with Multiple similar elements, Set will take care of removing those. See the above example for the element “Paris”

Set are created in Such as way that they don’t allow mutable Objects in them.

>>> cities = set (["hai","bye"])
>>> print cities
set(['hai', 'bye'])
>>> cities = set ({"hai","bye"))
  File "<stdin>", line 1
    cities = set ({"hai","bye"))
SyntaxError: invalid syntax

Empty Set - Creating an empty set is a bit tricky. Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements we use the set() function without any argument.

S = set()

Updating – Though Sets does not allow Mutable Objects, Sets are mutable objects as,

>>> cit=set(("hai","kai"))
>>> cit.add(("hello"))
>>> print cit
set(['hai', 'kai', 'hello'])

We can add single elements using the method add(). Multiple elements can be added using update() method. Theupdate() method can take tuples, lists, strings or other sets as its argument. In all cases, duplicates are avoided.

Frozen Sets – Frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned. While tuples are immutable lists, frozensets are immutable sets.

Sets being mutable are unhashable, so they can't be used as dictionary keys. On the other hand, frozensets are hashable and can be used as keys to a dictionary. Frozensets can be created using the function frozenset().

>>> cities = frozenset(["Frankfurt", "Basel","Freiburg"])
>>> cities.add("Strasbourg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
Read More

Saturday, July 18, 2015

Weblogic Overload Protection

Weblogic Server has a built-in feature for detecing ,avoding and recovering from overload conditions. So when a Weblogic server is experiencing Out of memory conditions or any of the Server Sub System is in failed state , we can instruct weblogic server to take corrective actions .  We can tune the Weblogic server to take corrective actions during these overload states. We can for example instruct to kill the server process during a failed or a overload state so that the node manager or administrator can take corrective actions.

A managed-server can fail as a result of out-of-memory exceptions or stuck application threads, or if one of its services is running into an error condition. A managed-server instance can monitor its health. If it detects that an unstable state is reached, it declares itself failed.

By usiong the Weblogic Over load protection feature we can prevent the negative consequences like degraded application performance and stability causing the server not to take any requests from users.

Configuring the Over load protection
Go to Server -> Overload tab to configure the over load protection for that server.

In the overload tab we can see 2 actions which can taken when we see a Over load condition

Panic action - When the kernel encounters a panic condition , the below actions can be taken.The following two actions are available.

Exit the server process

When the server health monitoring encounters a critical situation and flags the server as failed, one of three predefined actions can be taken automatically

"Force immediate shutdown of this server", meaning that server will shut down completely
"Suspend server for correction action", meaning that server will go into admin state

There are certain other conditions that can be used that exists in the same tab ,

Max Stuck Thread Time - The number of seconds that a thread must be continually working before this server diagnoses the thread as being stuck
Stuck Thread Count - The number of stuck threads after which the server is transitioned into FAILED state. There are options in OverloadProtectionMBean to suspend and shutdown a FAILED server. By default, the server continues to run in FAILED state.

Hope this helps about the over load protection in weblogic.
Read More

Apache - Host a YUM Repository

There will be cases where we need to download various packages from internet. in most of these cases the YUM repository will be hosted on a web server. In this article we will see how we can Host a YUM repository on a Apache web server.

1) Create a location mkdir /var/www/html/myrepo. The location /var/www/html is the Apache Document root and this is available in Apache configuration file httpd.conf. If you are going for a new location make the necessary changes

2) Copy some of the packages to the location and run the createrepo command

[root@vx111a test]# createrepo /var/www/html/myrepo
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 0 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Spawning worker 4 with 0 pkgs
Spawning worker 5 with 0 pkgs
Spawning worker 6 with 0 pkgs
Spawning worker 7 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

Once the createrepo is done, we can see a repodata directory in the same /var/www/html/myrepo

[root@vx111a myrepo]# ll
drwxr-xr-x. 2 root root    4096 Jul 17 18:52 repodata
-rw-r--r--. 1 root root 2489408 Jul 17 18:51 zsh-5.0.2-7.el7.x86_64.rpm

3) Once the repodata is created we need to set the permissions on the location as

chmod o+r /var/www/html/myrepo -R
chcon -R httpd_sys_content_t /var/www/html/myrepo

The above last command need to execute only if Selinux is in enforcing mode

4) Once the above steps are done we need to configure the repo file for the client location in /etc/yum.repos.d/myrepo.repo as
name=my custom repo
5) We can also access the apache server where we can download the packages too.

Hope this helps, More to come
Read More

Apache - mod_proxy_ajp

There are many other modules available in Apache which can connect to the back end servers like Tomcat. mod_proxy_ajp is on such Apache module. mod_proxy_ajp which can be used to forward a client HTTP request to an internal Tomcat application server using the AJP protocol. HTTP protocol uses a plain text format for communications. The plain text format where actual words make up the message can create a Overhead of traffic. AJP takes care of this by converting these messages into binary code thus reducing the amount of space taken by each message.

AJP or Apache JServ Protocol, is an optimized binary version of HTTP that is typically used to allow Tomcat to communicate with an Apache web server. So it is always better to use AJP protocol in communication between Apache and back end servers

Now the mpd_proxy_ajp module is used to reverse proxy to a back-end application server (e.g. Apache Tomcat) using the AJP13 protocol. The usage is similar to an HTTP reverse proxy, but uses the ajp:// prefix:

For this module to work we need another modules to be loaded into Apache which is mod_proxy. It provides support for the Apache JServ Protocol version 1.3 (hereafter AJP13).Thus, in order to get the ability of handling AJP13 protocol, mod_proxy and mod_proxy_ajp have to be present in the server.

In this article we will see how we can configure mod_prox_ajp module in Apache for 2 tomcat server running back end. Now here is the basic Virtual Host configuration done on my Apache server

LoadModule  mod_proxy_ajp  modules/

<VirtualHost myproject.local:8980>

    DocumentRoot /var/www/virtual/
    ServerName myproject.local
    ErrorLog logs/
    CustomLog logs/ common

    <Directory "/var/www/virtual/">
        Options None
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
        Allow from all

     ProxyRequests On
     <Proxy *>
          Order deny,allow
          Allow from all

     ProxyPass           /app   ajp://
     ProxyPassReverse    /app   ajp://

     ProxyPass           /app   ajp://
     ProxyPassReverse    /app   ajp://


The most important lines in the above configuration are,

ProxyPass           /app   ajp://
ProxyPassReverse    /app   ajp://

ProxyPass           /app   ajp://
ProxyPassReverse    /app   ajp://

The proxy pass element sends the request back to the application myApp/index.jsp which is running on the tomcat server running on So when ever we access the Apache Host URL with adding /app we hit the application running in the tomcat server . One more important thing in this case is that we need to configure the AJP protocol in the tomcat-server/conf/server.xml file.

Make sure you change the details in The The ProxyPassReverse is used to change the headers sent by the app  to Apache, before Apache sends it the browser. This element is used to modify headers before sending the response back to the browser.

In the above configuration we have the same application deployed in 2 tomcat servers running on different ips and ports. The application in any one of the tomcat servers will be accessed when we use to access them using http://myproject.local:8980/app.

Hope this helps, More to Come.

Read More