def QueryLog(jRecvData): """查询日志信息 参数: param1: 前端发送查询条件(json格式) 抛出异常: Exception:数据库异常 """ queryData = {} decodejson = json.loads(jRecvData) if getDbConf() != 0: print 'read dbconf error' return -4, queryData url = 'mysql+mysqldb://%s:%s@%s:%s/%s' % (dbconf['usrid'], dbconf['passwd'], dbconf['dbip'], dbconf['dbport'], dbconf['logdbname']) try: #检测日志数据库是否存在 if not database.database_exists(url): return -1, queryData #连接数据库 dbconf['dbport']=int(dbconf['dbport']) conn = MySQLdb.connect(db=dbconf['logdbname'], user=dbconf['usrid'], passwd=dbconf['passwd'], host=dbconf['dbip'], port=int(dbconf['dbport'])) print "===========217" print conn except Exception, e: print e return -2, queryData
def main(): config = config_from_xml_file(CONFIG_FILE) mysql_config = copy.deepcopy(config['mysql_config']) del mysql_config[ 'database'] # databases can't be checked or created if a database is specified database_connection = create_database_connection(mysql_config) database_cursor = create_database_cursor(database_connection) if database_exists(config, database_cursor): print("'{}' database already exists.".format(config['database_name'])) else: create_database(config, database_cursor) print() database_cursor.stop_cursor() database_connection.disconnect() database_connection = create_database_connection(config['mysql_config']) database_cursor = create_database_cursor(database_connection) for table in config['tables'].keys(): if table_exists(table, database_cursor): print("'{}' table already exists.".format(table)) else: create_mysql_table(config, table, database_connection, database_cursor) print("'{}' table has been created.".format(table)) print("Populating '{}' table with data......".format(table)) load_data_into_mysql_table(config, table, database_connection, database_cursor) print("Population of '{}' table has been completed".format(table)) print() database_cursor.stop_cursor() database_connection.disconnect()
def handle_mssg(mssg): # Unexpected bit: # When the bot sends a message, this function is called. If the sender is # itself, quit, to avoid an infinite loop. if mssg['sender_email'] == client.email: return print 'Message received!' content = mssg['content'].strip() first = content.split()[0] strip_len = len(first) while first.endswith(':'): strip_len += 1 first = first[:-1] content = mssg['content'][strip_len:] content = content.lstrip() # First, assume the message is a "ssh" request. shh = first if shh.lower() == 'shh' or shh.lower() == 'ssh': # Allow for the ssh pun...because it's fun anon_shh(stream) return # Next, assume first is the recipient of an anonymous message recipient_email = first if recipient_email in emails: sender_email = mssg['sender_email'] # Generate code, send it to person code = gen_new_relay_code() if not database.database_exists(filename): print 'Database %s doesn\'t yet exist. Creating it now...'%(filename) database.create_database(filename) if not database.table_exists(table, filename): # TODO: Put this block of code into the database module as the create_table function # The current problem is that I need a way of passing a tuple of strings, and # I'm not sure how to securely do this... # database.create_table(cols, table, database) where cols = (col1, col2) with sqlite3.connect(filename) as con: c = con.cursor() c.execute("CREATE TABLE %s (code TEXT, email TEXT)"%(table)) database.add_record((code, sender_email), table, filename) end_content = '\n\nThis sender\'s key is %s. To reply, send a message in the following format:\n' %(code) end_content += '%s: <your_message>' %(code) content += end_content send_anon_pm(recipient_email, content) send_anon_pm(sender_email, 'Anon message sent to %s' %(recipient_email)) return # Next, assume first is the code of a relax message code = first email_match = None if database.database_exists(filename): email_match = search_for_code(code) if email_match: sender_email = mssg['sender_email'] content = 'Response from %s:\n'%(sender_email) + content end_content = '\nTo reply anonymously, send a message in the following format:\n' end_content += '"%s: <your_message>" (without the quotes)' %(sender_email) content += end_content send_anon_pm(email_match, content) send_anon_pm(sender_email, 'Your reply was sent to %s.' %(code)) return # All assumptions exhausted, reply with help message. sender_email = mssg['sender_email'] send_anon_pm(sender_email, get_help_response())
def search_for_code(code): if database.database_exists(filename): pdb.set_trace() result = database.lookup_record(code, 'code', table, filename) if result: return result[0][1]