Python check multiple variables for none


I have multiple variables in my python script.

I would like to check if any variable is None out of my variables.

Following are my variables:

a = 'a'
b = 'b'
c = None

Python has a built in method called “all” which return True if all elements of the iterable are true (or if the iterable is empty).

So here I am using all method to check if any declared variables are None. I am using following code:

print all([a, b, c])



print all([a, b])



If we observe above two print statements with all method, first statement output is False since variable c is None and second statement output is True since variables a and b are not None.

Heartbleed Ubuntu 12.04 – 64 bit fix


What is HeartBleed?

The Heartbleed Bug is a serious vulnerability in the popular OpenSSL cryptographic software library. This weakness allows stealing the information protected, under normal conditions, by the SSL/TLS encryption used to secure the Internet. SSL/TLS provides communication security and privacy over the Internet for applications such as web, email, instant messaging (IM) and some virtual private networks (VPNs).

The Heartbleed bug allows anyone on the Internet to read the memory of the systems protected by the vulnerable versions of the OpenSSL software. This compromises the secret keys used to identify the service providers and to encrypt the traffic, the names and passwords of the users and the actual content. This allows attackers to eavesdrop on communications, steal data directly from the services and users and to impersonate services and users.

Affected OpenSSL versions:

Only 1.0.1 and 1.0.2-beta releases of OpenSSL are affected including 1.0.1f and 1.0.2-beta1.

How to check your OpenSSL Version?

Use following command to know your OpenSSL version:

$ openssl version -a

If you are using any of above mentioned affected versions of OpenSLL please upgrade your OpenSSL.

How to upgrade OpenSSL?

I used following script to upgrade OpenSSL to fixed vision:

openssl version -a
apt-get install zlib1g-dev -y
dpkg -i libssl1.0.0_1.0.1-4ubuntu5.12_amd64.deb
dpkg -i libssl-dev_1.0.1-4ubuntu5.12_amd64.deb
dpkg -i openssl_1.0.1-4ubuntu5.12_amd64.deb
rm libssl1.0.0_1.0.1-4ubuntu5.12_amd64.deb libssl-dev_1.0.1-4ubuntu5.12_amd64.deb openssl_1.0.1-4ubuntu5.12_amd64.deb
openssl version -a

After execution of above script reboot your machine, unless you reboot machine OpenSSL version won’t change.

After upgrade your OpenSSL version should look like following:

$ openssl version -a


OpenSSL 1.0.1 14 Mar 2012
built on: Mon Apr 7 20:33:29 UTC 2014
platform: debian-amd64

Python concatenate/joining list items


By using join function we can concatenate or join all items with a delimiter(all items in list must strings) in a list.

I have a list as ['a', 'b', 'c'] and joining each value with space delimiter in following example:

>>> list = ['a', 'b', 'c']
>>> " ".join(list)
'a b c'

To join with comma delimiter:

>>> ",".join(list)

Similarly we can use join or concatenate all values in list. The only condition here is all values in list must strings.

HAProxy – Starting proxy webfarm: cannot bind socket

Today morning when I am trying to start haproxy in one of server I got following error:

[ALERT] 096/213653 (26549) : Starting proxy webfarm: cannot bind socket

I have spent around 2 hours to debug this issue.

Here is the solution:

HAProxy will try to bind to Virtual IP which will only available in active node. Since our virtual IP is already bind to active node, this server unable to bind.

To resolve this issue we need to append net.ipv4.ip_nonlocal_bind=1 in /etc/sysctl.conf file.

Open /etc/sysctl.conf and append following line:


After adding above line run following command to check if its updated or not:

$ sysctl -p


net.ipv4.ip_nonlocal_bind = 1

In output of sysctl -p we can see net.ipv4.ip_nonlocal_bind = 1.

Now start HAProxy:

$ service haproxy restart

* Restarting haproxy haproxy

After starting check HAProxy status:

$ service haproxy status

haproxy is running.

To find where is your virtual IP

To find to which host your virtual IP is bind use following command:

$ ip addr

In output check if you can find your virtual IP.

Start HAProxy with init script

To start HAProxy with init script set the ENABLED option to in /etc/default/haproxy file

Open /etc/default/haproxy file

$ vim /etc/default/haproxy

Append following line:


After adding above line restart HAProxy.

$ service haproxy restart 

* Restarting haproxy haproxy

vim convert to lowercase or uppercase


By using simple substitution we can convert  text in vim either to lowercase or to uppercase.

I have a following text in in one of file:


To convert complete text to uppercase use following substitution code:




Similarly to convert complete text to lowercase use substitution code:




Python psutil – install recent version in Ubuntu

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap.

To install recent version of psutil first install python-dev module by using following command:

$ sudo apt-get install python-dev

Now install psutil with pip.

$ sudo pip install psutil 

To upgrade psutil to recent version use following command:

$ sudo pip install psutil --upgrade

By any chance if there is no pip module in your machine, install pip with following command:

$ sudo apt-get install python-pip

I tested above commands in Ubuntu 12.04 LTS with python 2.7 version.

psutil/_psutil_linux.c fatal error: Python.h: No such file or directory

Recently while trying to install recent version of psutil I got following error:

psutil/_psutil_linux.c:12:20: fatal error: Python.h: No such file or directory
compilation terminated.

After debugging about the issue for a while I got to know that to install psutil recent version, it required python-dev module pre installed.

Use following command to install python-dev

$ sudo apt-get install python-dev

After installing python-dev psutil got installed without any issue.

Python file operations – read/write/append


To read and existing file use following code. In this code I am trying to read inputFile, it will throw IOError if given input file is not available.

import os, sys
#Opening file in read mode
f = open("inputFile", 'r')
#Reading all lines in input text as list
lines = f.readlines() 
print lines
for line in lines:
    #Printing each line in list
    print line 

To write output to a file use following code. In this code I am opening file with write mode using ‘w’. In write mode it will create input file if it dos not exists and write content to it or if input file exists it will overwrite content of input file with new text.

import os, sys
#Opening file in write mode
f = open("inputFile", 'w') 
#Writing hello to input file

To append output to existing file use we need to open file with append mode using ‘a’. In following code I am opening file with append mode. In append mode it append content to input file if it exists else it will create input file and append content to it.

import os, sys
#Opening file in append mode
f = open("inputFile", 'a') 
#Appending hello to input file.

Python convert string to list/tuple/dictionary

By using ast modules literal_eval function we can convert any python string to valid literal.

ast modules literal_eval function will only evaluates string that are valid python literals.

Lets try with an example:

I have file with some valid python literals and I want to evaluate each of them.

Content of my input file is following:

[1, 2, 3]
(1, 2, 3)
{‘a’:1, ‘b’:2, ‘c’:3}

Here I want to detect type of literal in each line.

First I will try with a simple example:

import os, sys
f = open(sys.argv[1], 'r')
lines = f.readlines()
print lines
for i in lines:
    print i.strip(), type(i)


[1, 2, 3] <type ‘str’>
(1, 2, 3) <type ‘str’>
{‘a’:1, ‘b’:2, ‘c’:3} <type ‘str’>
‘abc’ <type ‘str’>
123 <type ‘str’>

Just observer output, by default all lines are detected as string.

Now try with ast modules literal_eval function.

import os, sys, ast
f = open(sys.argv[1], 'r')
lines = f.readlines()
#print lines
for i in lines:
    print i, type(ast.literal_eval(i))


[1, 2, 3] <type ‘list’>
(1, 2, 3) <type ‘tuple’>
{‘a’:1, ‘b’:2, ‘c’:3} <type ‘dict’>
‘abc’ <type ‘str’>
123 <type ‘int’>

Now observer above output, each line is detected and converted as its own literal type.

I tested this example in Python 2.7.3

Perl – Can’t locate XML/ in @INC

Recently while I am trying to reuse a perl scrip I got an error saying “Can’t locate XML/ in @INC”.

Following is complete error message:

Can’t locate XML/ in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .)

After debugging for a while I got to know is XML/ module is available in “libxml-simple-perl”, I installed libxml-simple-perl module and re-executed script, the script worked without any issue.

Use following command to install libxml-simple-perl module:

$ sudo apt-get install libxml-simple-perl