Using Python to connect to Oracle Database, export CDR file in txt format and push to FTP Server

Tram Ho

A business that you who operate application systems or have to perform is to push CDR between systems to serve the following tasks: reconciliation, report … etc. Today I will write a tutorial about using Python to do that.

Execution model:

  1. Database Oracle stores the data of the system applications it manages.
  2. My FTP server is given account / directory information by another party to push the CDR up.
  3. Data export from Oracle Database writes to a txt format file and pushes it to a folder on the FTP Server (the folder is formatted in yyyy-mm), in case it will automatically create a new folder in the next month before pushing the CDR file.

image.png

In the Python code I use libraries (how to install more libraries I have noted in the code):

  • configparser for reading variable values ​​in properties file
  • cx_Oracle for Oracle Database connection
  • ftplib connect to FTP Server

Contents properties file: config.properties

Main.py file content Source code content: import library, read values ​​from properties file

Source code content: check if the export file path on the server exists will make oracle database connection, run the command to export the defined data, write to the txt format cdr file (variable values ​​in the cdr file are separated by commas. ,)

Source code content: push cdr file to ftp server, in case the directory on ftp server has not been initialized, will initialize the directory with the name ‘yyyy-mm’ before pushing the cdr file.

That’s the job done, when you put the crontab to run you can record logs for the monitor job to push the CDR on.

Share the news now

Source : Viblo