Monday, October 23, 2017

Parsing configuration files using ConfigParser in Python

ConfigParser is used to parse configuration file easily in python. A configuration file  can be grouped by  sections with [section] header,  and entries can be added as in name: value  order.

Eg: settings.cfg



In python;

import ConfigParser

  def loadSettings(self):
        config = ConfigParser.ConfigParser()'./config/settings.cfg')  
        if config.has_section('CustomerSettings'):
            if len(config.options('CustomerSettings')) >0:
                for key,value in config.items('CustomerSettings'):
                    print value

        if config.has_section('LogSettings') :
            if len(config.options('LogSettings')) >0:            
                filepath = config.get('LogSettings', config.options('LogSettings')[0])
                print filepath  

Overcoming "No 'Access-Control-Allow-Origin' header is present on the requested resource." in jQuery

When calling a backend service via cross domain, if we do not set CORS header we will get above javascript error in browsers.
To overcome this issue in jQuery, we can set headers parameter.

type : 'GET',
url :,
headers : {
'Access-Control-Allow-Origin' : '*'
success : function(data, textStatus) {
error : function(xhr, textStatus, errorThrown) {
At the same time, server too needs to allow cross domain requests to be served.

We can set request headers at server side..

  request.setHeader('Access-Control-Allow-Origin', '*') 
  request.setHeader('Access-Control-Allow-Methods', 'GET')
  request.setHeader('Access-Control-Allow-Headers', 'x-prototype-version,x-requested-with')