Linux

Python Configparser

In the industry level code, we don't hardcode the variables. Sometimes One code needs to be executed on different servers. Consider if you hardcode the IP address of server inside the code then you need to change the code according to the server. if the code is going to be deployed in the 100 servers. it is very tedious to change the code and commit the code on GitHub. To overcome this problem of hardcoding we use configparser.

In order to use configparser we need to import the module ConfigParser. The ConfigParser module has been renamed to configparser in Python 3. Let us take a simple example.
First, we create an ini configuration file.
Let us take the example of servers SQL, DNS, and web-server. We put the configuration in the server_config.ini

server_config.ini:

[SQL]
Server_ip= "127.0.0.1"
Port = 3306

[DNS]
Server_ip= "127.0.0.1"
Port = 53

[WebServer]
Server_ip= "127.0.0.1"
Port = 80

In the above config file, we have fixed the different server IPs and ports. By using [
] we have created the section in the config file like SQL, DNS, and web-server sections.

Let us see how to use the ini file in the code.
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("server_config.ini")
SQL_Server_IP = config.get("SQL", "Server_ip")
SQL_Port = int(config.get("SQL", "Port"))
print "SQL server IP and Port ", SQL_Server_IP,"\t", SQL_Port

DNS_Server_IP = config.get("DNS", "Server_ip")
DNS_Port = int(config.get("SQL", "Port"))
print "DNS server IP and Port ", DNS_Server_IP,"\t", DNS_Port

Web_Server_IP = config.get("WebServer", "Server_ip")
Web_Port = int(config.get("WebServer", "Port"))
print "Web server IP and Port ", Web_Server_IP,"\t", Web_Port


By using the syntax config = ConfigParser.ConfigParser(), the config object get created. In the syntax config.read("server_config.ini"), the read method is used to read the config file. The syntax config.get("SQL", "Server_ip") return the value of "Server_ip" of SQL server.
Let us see the output.
Python ConfigParser
Python ConfigParser Example

Let us see another code.

import ConfigParser
config = ConfigParser.ConfigParser()
config.read("server_config.ini")
for section in config.sections():
   print section,config.options(section)
   for name, value in config.items(section):
      print ' %s = %s' % (name, value)
      print "SQL server IP and Port ", SQL_Server_IP,"\t", SQL_Port


Let us see the output.
Python ConfigParser sections
Python ConfigParser section Example

The syntax config.sections() return the list of all sections like ['SQL', 'DNS', 'WebServer']. The syntax config.options(section) return the a list of all parameters of particular section. The syntax config.items(section) return the parameter and parameter's values in the form of the list of tuples. Example config.items("SQL") return [('server_ip', '"127.0.0.1"'), ('port', '3306')].

Write the config file

So far we have seen how to read the config files, now we will see how to write the config file. We will use Python code to write the config file. Consider you want to add a new section in the existing config file.
import ConfigParser
config = ConfigParser.ConfigParser()
file_w = open("server_config.ini",'a+')
config.add_section('FTP')
config.set('FTP','Server_ip','127.0.0.1')
config.set('FTP','Port',21)
config.write(file_w)
file_w.close()


The code is easy to understand, just create the object of ConfigParser.ConfigParser() and open the existing file in append mode. By using syntax config.add_section('FTP') add the new section "FTP". By using config.set() add new parameter in the config file.
Run the code the and see the config file.