Question
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
import osimport re
import sys
import logging
import optparse
import MySQLdb
import subprocess
"""Script default configuration
"""
DB_HOST = '127.0.0.1'
DB_PORT = '3306'
DB_USER = 'wlanuser'
DB_PASS = 'wlanpass'
DB_NAME = 'wlandb'
class DB:
"""MySQL wrapper class
"""
def __init__(self, ahost = DB_HOST, aport = DB_PORT, auser = DB_USER, apasswd = DB_PASS, adb = DB_NAME):
"""Initialize database configuration
"""
self.conn = None
self.host = ahost
self.port = aport
self.user = auser
self.passwd = apasswd
self.db = adb
def connect(self):
"""Connect to database
"""
self.conn = MySQLdb.connect(host=self.host,port=int(self.port),user=self.user,passwd=self.passwd,db=self.db,use_unicode=True, charset="utf8")
self.conn.autocommit(True);
def cursor(self):
"""Get current cursor of database
"""
try:
return self.conn.cursor()
except (AttributeError, MySQLdb.OperationalError):
self.connect()
return self.conn.cursor()
def escape(self, str):
"""Returns escaped string
"""
if self.conn == None:
self.connect()
return self.conn.escape_string(str)
def query(self, sql):
"""Query database with SQL statement
"""
try:
cursor = self.conn.cursor()
cursor.execute(sql)
except (AttributeError, MySQLdb.OperationalError):
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql)
return cursor
def close(self):
"""Close connection
"""
if self.conn != None:
self.conn.close()
def commit(self):
"""Manual commit database operation
"""
self.conn.commit()
def parse_command_line(argv):
"""Command line options parser for the script
"""
usage = "usage: %prog [options]"...