class MongodbConn(object): def __init__(self, db_name, host_name=settings.hostname): """ init the common db connection transactions. """ self.client = mongo_client.MongoClient(host=host_name) self.db = self.client.__getattr__(db_name) #init all the collections #set attr self.colname = db.col self.col_name_dict = DBCol().__dict__ for i in range(len(self.col_name_dict)): self.__setattr__( self.col_name_dict.keys()[i], self.db.__getattr__(self.col_name_dict.values()[i])) def get_db(self): return self.db def close_con(self): """ close the db connection. """ self.client.close() def set_cur_col(self, file_name): self.cur_col = self.db.__getattr__(file_name) return self.cur_col def get_next_col(self): for col_name in self.col_name_dict: yield self.set_cur_col(col_name)
class MongodbConn(object): def __init__(self, db_name, host_name=settings.hostname): """ init the common db connection transactions. """ self.client = mongo_client.MongoClient(host=host_name) self.db = self.client.__getattr__(db_name) #init all the collections #set attr self.colname = db.col self.col_name_dict = DBCol().__dict__ for i in range(len(self.col_name_dict)): self.__setattr__(self.col_name_dict.keys()[i],self.db.__getattr__(self.col_name_dict.values()[i])) def get_db(self): return self.db def close_con(self): """ close the db connection. """ self.client.close() def set_cur_col(self,file_name): self.cur_col = self.db.__getattr__(file_name) return self.cur_col def get_next_col(self): for col_name in self.col_name_dict: yield self.set_cur_col(col_name)
def save_to_ip_table(): col_names = DBColNames().col_names() print type(col_names) #for i in col names ipcount = 0 stepby = 0 x = None for col_name in col_names: print col_name for ip in find_ip_from_collection(collection_name=col_name): if is_ip_resolved(ip): stepby += 1 if stepby % 100 == 0: print "stepby:", stepby else: try: x = IpLocationFetch(ip).json_data_stream except ConnectionError: import time time.sleep(120) x = IpLocationFetch(ip).json_data_stream dic = IpLocation(x).get_dict_data() ip_db.__getattr__(ip_db_col_name).insert(dic) ipcount += 1 if ipcount % 100 == 0: print "ipcount:", ipcount
def __init__(self, db_name, host_name=settings.hostname): """ init the common db connection transactions. """ self.client = mongo_client.MongoClient(host=host_name) self.db = self.client.__getattr__(db_name) #init all the collections #set attr self.colname = db.col self.col_name_dict = DBCol().__dict__ for i in range(len(self.col_name_dict)): self.__setattr__(self.col_name_dict.keys()[i],self.db.__getattr__(self.col_name_dict.values()[i]))
def __init__(self, db_name, host_name=settings.hostname): """ init the common db connection transactions. """ self.client = mongo_client.MongoClient(host=host_name) self.db = self.client.__getattr__(db_name) #init all the collections #set attr self.colname = db.col self.col_name_dict = DBCol().__dict__ for i in range(len(self.col_name_dict)): self.__setattr__( self.col_name_dict.keys()[i], self.db.__getattr__(self.col_name_dict.values()[i]))