In this example I am aiming to keep the IP address, Port Number, Username and Password in a separate file, so when a connection to a database is called from the main script, they cannot be seen or manipulated.
The folder structure is as follows:
The main.py file is where the connection to the Database will be called. Its contents is as follows:
from Database import * c = connector.Connect() print c
The statement from Database import * will call all modules from the sub directory Database.
The statement connector.Connect() is instantiating the class Connect into the Python object called c. The class Connect is located in the Connector.py module.
Looking at the contents of the Database subdirectory, it contains 2 files:
The primary use of __init__.py is to initialize Python packages. in this case, it contains the following statement:
__all__ = ["connector"]
__all__ is a list containing the names of modules that you want to be imported with import *. What this means is that if you have other modules located in the Database sub folder, you can selectively control what modules that you want to import using the * operator.
The Connector.py module is where the Connect() class is stored and called from. It contains the following statements:
import pymysql class Connect(object): def __init__(self): try: self.conn = pymysql.connect(host=localhost, port=portnumber, user=username, passwd=password) except: print("ERROR :: could not connect to MySQL Database. Check:\n1. that the firewall is not blocking the connection\n2. host address is correct\n3. port number is correct\n4. user name is correct\n5. password is correct\n6. casting of conn variable")
For the Connector.py module to work, you must firstly have the pymysql modules installed. The statement import pymysql imports modules from pymysql.
The statement class Connect(object): declares the class Connect as an object.
The statement def function acts as a constructor of sorts and initialises the class, in this case by creating a variable called conn and storing the connection to the MySQL database in it. This variable is attached to any instance of the Connect class.
Further, I had placed the creation of the conn variable within a try/except block for error catching purposes.
The values of the arguments in pymysql.connect() are place holders for security purposes. You should replace the following values:
localhost: a string value that is either a IPv4 address (e.g. ‘127.0.0.1’) or ‘localhost’
port: a integer value that is the TCP/IP port number used by the MySQL database server.
user: a string value of a users name recognised by the MySQL database server.
passwd: a string value of a password that is mapped to the above users name and is recognised by the MySQL database server.
To recap, when the mian.py script is run, it will create an instance of the Connect Class and store is as an object in the c variable.
The c variable will store a open connection to a MySQl database which the can be used to inject or extract content from the database.