def showinfo(): db = dbpond.Database() result = db.fetchone("SELECT COUNT(*) FROM `smnt_client`;") print("总用户数:%d" % result[0]) result = db.fetchone( "SELECT COUNT(*) FROM (SELECT COUNT(*) FROM `smnt_client` GROUP BY `officeid`) AS oids;" ) print("总办公室数:%d" % result[0])
def userdel(userid): if not userid.isdigit(): logging.warn("requireInteger:%s" % userid) return None db = dbpond.Database() result = db.execute("DELETE FROM `smnt_client` WHERE (`id`=%s)", (userid)) if result > 0: logging.info("删除成功:%s" % result) else: logging.info("删除失败:%s" % result) return True
def useradd(officeid, realname, passbase): db = dbpond.Database() username = '' password = hashlib.md5(passbase.encode('utf8')).hexdigest() sql = "INSERT INTO `smnt_client` (`unxtime`) VALUES(UNIX_TIMESTAMP());" userid = db.insert(sql) userkey = '%s%d' % (password[:16], userid) sql = "UPDATE `smnt_client` SET `userkey`=%s,`officeid`=%s,`username`=%s,`realname`=%s,`password`=%s,`passbase`=%s WHERE `id`=%s;" result = db.execute( sql, (userkey, officeid, username, realname, password, passbase, userid)) if result == 1: return userkey return None
def userfind(fkey, fval): #print("userfind -%s %s"%(fkey, fval)) db = dbpond.Database() if fkey == 'n': #用户名 sql = "SELECT `id`,`userkey`,`officeid`,`realname`,`lastmac`,`addtime` FROM `smnt_client` WHERE `realname`=%s" result = db.fetchall(sql, (fval)) for item in result: print(item[0], item[1].decode('utf8'), item[2].decode('utf8'), item[3].decode('utf8'), item[4].decode('utf8'), item[5].strftime('%F %T')) elif fkey == 'i': #用户编号 pass elif fkey == 'k': #用户密钥 pass elif fkey == 'o': #办公室编号 pass else: print("type -%s error" % fkey)
def handle(self): logging.info("[服务端]与客户端%s建立连接" % (self.client_address, )) #一次accept链接会话 sw = worker.SocketWorker(self.request) handshake = sw.recv(decoding='json') #远程套接字地址端口元组 remotehp = self.request.getpeername() #握手信息 userkey, hostmac, hostname = handshake.get('userkey'), handshake.get( 'hostmac'), handshake.get('hostname') logging.debug('%s[%s]%s' % (hostmac, userkey, hostname)) if userkey is None or len(userkey) == 0: logging.warn("userkey is empty") return False #去数据库鉴权 db = dbpond.Database() result = db.fetchone( "SELECT `id`,`realname` FROM `smnt_client` WHERE `userkey`=%s LIMIT 1;", userkey) if result is None: #鉴权失败:用户不存在 sql = "INSERT INTO `smnt_client_autherr` (`userid`, `userkey`, `hostmac`, `hostname`, `content`, `remote`) VALUES (%s, %s, %s, %s, %s, %s)" db.insert(sql, (userkey[16:], userkey, hostmac, hostname, str(handshake), str(remotehp))) sw.send({ 'status': 2, 'message': 'authFailed:userNotFound' }, encoding='json') logging.info("authFailed:userNotFound,connectionClosed") return None else: #鉴权成功 sw.send({ 'status': 1, 'message': 'authSuccess:%s' % userkey[16:] }, encoding='json') #记录behavior sql = "INSERT INTO `smnt_behavior` (`userid`, `userkey`, `action`, `content`, `hostmac`, `hostname`, `remote`) VALUES (%s, %s, %s, %s, %s, %s, %s)" db.insert(sql, (userkey[16:], userkey, 'logout', '用户连接成功', hostmac, hostname, str(remotehp))) userid = result[0] realname = result[1].decode('utf-8') while True: try: info = sw.recv(decoding='json') logging.info('%s[%s]=%s' % (realname, userid, str(info['filename']))) #时区的坑,localtime当地时区的结构化时间格式 filetime = time.localtime(info['filetime']) filename = time.strftime('%Y%m%d%H%M%S', filetime) ymd = time.strftime('%Y%m%d', filetime) filedir = '{basedir}{sep}uploads{sep}{userid}{sep}{ymd}'.format( basedir=basedir, sep=os.sep, userid=userid, ymd=ymd) filepath = '{filedir}{sep}{filename}.png'.format( filedir=filedir, sep=os.sep, filename=filename) # 判断并创建目录 if not os.path.isdir(filedir): try: os.makedirs(filedir) except: pass sw.recvfile(filepath, info['filesize']) except ConnectionResetError: logging.info("[控制台]客户主机%s强迫关闭现有连接" % (self.client_address, )) break except Exception as reason: logging.info("[控制台]handleException:%s" % reason) break finally: pass logging.info("[控制台]与客户端%s断开连接" % (self.client_address, )) #记录behavior sql = "INSERT INTO `smnt_behavior` (`userid`, `userkey`, `action`, `content`, `hostmac`, `hostname`, `remote`) VALUES (%s, %s, %s, %s, %s, %s, %s)" db.insert(sql, (userkey[16:], userkey, 'login', '用户断开连接', hostmac, hostname, str(remotehp)))