python整理

pyhton实例: 批量添加用户

2017-06-16  本文已影响30人  随风化作雨
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import MySQLdb

class Subscriber(object):
    
    def __init__(self,host,db='HSS',port=3306,user='root',passwd=''):
        
        self.host = host
        self.port = port
        self.user = user
        self.passwd = passwd
        self.db = db
        self.sub_num = 10
        self.plmn = '12345'
        self.index_base = 1000
        self.imsi_list = []
        self.index_list = []
        
                for i in range(1,self.sub_num+1):
                        if len(self.plmn)==5:
                                self.imsi = self.plmn + "{0:010d}".format(i)
                        elif len(self.plmn)==6:
                                self.imsi = self.plmn + "{0:09d}".format(i)
                        self.index = str(self.index_base+i)

            self.imsi_list.append(self.imsi)
            self.index_list.append(self.index)


    def add(self):
        self.__conn = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db)
        self.__cur = self.__conn.cursor()
        for i in range(len(self.index_list)):
            
            self.__cur.execute("insert into APN_SELECTION values(%s,%s,'1','1','OFF','N','','N','')" %(self.index_list[i],self.imsi_list[i]))
            self.__cur.execute("insert into SUBSCRIBERINFO values(%s,%s,'00112233445566778899AABBCCDDEEFF','1',\
                       '000102030405060708090A0B0C0D0E0F','000102030405060708090A0B0C0D0E0F','8000',\
                       '000000000021','32','0','30D4000','61A8000','0','0','null','0','15',\
                       'apn01.ggsn-cluster-A.provinceB.mnc042.mcc311.gprs','0800','256','3000','1','0','BAICELLS')" %(self.index_list[i],self.imsi_list[i]))
    
        self.__cur.close()
        self.__conn.commit()
        self.__conn.close()
    
    def delete(self):
        self.__conn = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db)
                self.__cur = self.__conn.cursor()
                for i in range(len(self.index_list)):
                        
                        self.__cur.execute("delete from APN_SELECTION where RID=%s" %self.index_list[i])
            self.__cur.execute("delete from SUBSCRIBERINFO where RID=%s" %self.index_list[i])
                        
                self.__cur.close()
                self.__conn.commit()
                self.__conn.close()


    def check(self):
                self.__conn = MySQLdb.connect(host=self.host,port=self.port,user=self.user,passwd=self.passwd,db=self.db)
                self.__cur = self.__conn.cursor()
                self.__cur.execute("select * from APN_SELECTION")
        self.result1 = self.__cur.fetchall()
        self.__cur.execute("select * from SUBSCRIBERINFO")       
                self.result2 = self.__cur.fetchall()
        for data in self.result1:
                print data
        for data in self.result2:
            print data

                self.__cur.close()
                self.__conn.commit()
                self.__conn.close()


def main():

    mysub = Subscriber(host='192.168.9.242')
    mysub.add()
    mysub.check()
    #mysub.delete()


if __name__ == '__main__':
    
    main()
上一篇下一篇

猜你喜欢

热点阅读