| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #  | 
| ##@package  | 
| #  | 
| # @todo:   | 
| #  | 
| # @author: Alee  | 
| # @date 2019-2-20 ÏÂÎç04:50:04  | 
| # @version 1.0  | 
| #  | 
| # @note:   | 
| #  | 
| #---------------------------------------------------------------------  | 
| #---------------------------------------------------------------------  | 
| from bottle import Bottle, request  | 
| from lib import mylog  | 
| import os  | 
| import md5  | 
| import smtplib  | 
| from email.mime.text import MIMEText  | 
| from email.header import Header  | 
| import datetime  | 
| import subprocess  | 
| import urllib  | 
| # get: request.query.username request.GET.get('username','')  | 
| # post: request.forms.get('username')  request.POST.get('username')  | 
| #===============================================================================  | 
| # @myapp.route('/cool/kk/:name3/:count#\\d+#')  | 
| # def maybe(name3, count):  | 
| # client_ip = request.environ.get('REMOTE_ADDR')  | 
| # client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')  | 
| #===============================================================================  | 
|   | 
| g_AllMapCnt = 0  | 
|   | 
| myapp = Bottle()  | 
|   | 
| # ¿ÉÖ´Ðй̶¨µÄÔ¶³ÌÃüÁî  | 
|   | 
| @myapp.route('/control/openandclose.php', method='POST')  | 
| def RemoteCmd():  | 
|     dataDict = request.POST  | 
| #    for key in dataDict:  | 
| #        print key  | 
| #        mylog.debug("---%s"%dataDict[key])  | 
|     result = dataDict.get("control", "")  | 
|     if not result:  | 
|         return  | 
|       | 
|     sign = dataDict.get("sign", "")  | 
|     if not sign:  | 
|         return  | 
|       | 
|     ofile = open(r".\key.txt")  | 
|     text = ofile.read().strip()  | 
|     ofile.close()  | 
|     if sign != md5.md5("%s1234666%s"%(result, text)).hexdigest():  | 
|         mylog.debug("Ç©Ãûʧ°Ü")  | 
|         return  | 
|       | 
|     if result == "open":  | 
|         pResult = os.popen('tasklist /FI "IMAGENAME eq EventServer.exe"')  | 
|         pResult = pResult.read()  | 
|         if "PID" in pResult or "pid" in pResult:  | 
|             return " =======·þÎñÆ÷ÒѾ¿ªÆô£¬ÇëÔÙ´ÎÈ·ÈÏ·þÎñÆ÷Çé¿ö£¬ EventServer.exeÔËÐÐÖÐ"  | 
|         #os.system(r"call D:\ProjectServer\LaunchServerManager\Õý³£¿ª·þ.bat")  | 
|         # system call»áµ¼ÖÂ×Ó½ø³ÌÕ¼Óø¸½ø³ÌµÄÎļþÃèÊö·ûµ¼Ö¶˿ÚÎÞ·¨ÊÍ·Å£¬ Ó°ÏìÖØÆôʹÓà  | 
|         # Èç¹ûÐèÒª±£³Ö×Ó½ø³Ì³ÖÐø³¤ÆÚÔËÐеÄÓ¦¸Ãµ÷Óàsubprocess.Popen(cmd, close_fds=True)  | 
|         cmd = r"D:\ProjectServer\LaunchServerManager\Õý³£¿ª·þ.bat"  | 
|         subprocess.Popen(cmd, close_fds=True)  | 
|         return "open success"  | 
|     elif result == "close":  | 
|         # ¼ÇµÃ¹Ø·þºó»òÕßÖØÆô»úÆ÷Ðè×Ô¶¯Æô¶¯·þÎñÆ÷¿ØÖÆÌ¨  | 
|         os.system(r"call D:\ProjectServer\LaunchServerManager\Õý³£¹Ø·þ.bat")  | 
|         return "close success"  | 
|       | 
|     return  | 
|   | 
| # OnLogError OnPyError  | 
| # ·þÎñÆ÷¿ªÆôµÄ״̬»ã±¨  | 
| @myapp.route('/control/state.php')  | 
| def ServerState():  | 
|     global g_AllMapCnt  | 
|     try:  | 
|         dataDict = request.GET  | 
|         stateType = dataDict.get("Type", "")  | 
|         if not stateType:  | 
|             return  | 
|         if stateType == "MapInit":  | 
|             # ·þÎñÆ÷¹Ø±ÕµÄʱºò»áÖØÆô£¬´Ë״ֻ̬ÓÃÓÚ¿ª·þµÄʱºòÊÇ·ñ³õʼ»¯OK  | 
|             g_AllMapCnt = dataDict.get("MapCount", 0)  | 
|             if g_AllMapCnt:  | 
|                 ifile = open(".\MapCount.txt", "w")  | 
|                 ifile.write(str(datetime.datetime.today()) + "\t·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý=%s"%g_AllMapCnt)  | 
|                 ifile.close()  | 
|             #return "·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý=%s"%dataDict.get("MapCount", 0)  | 
|             return  | 
|           | 
|         elif stateType in ["MapError", "GameServerError", "PyMongoError"]:  | 
|             #groupID=%s&userDBName=%s&Type=PyMongoError  | 
|             ServerInfo = "´íÎóÀàÐÍ:%s ·þÎñÆ÷×é:%s, dbname:%s"%(stateType,  | 
|                                                        dataDict.get("groupID", 0),  | 
|                                                        dataDict.get("userDBName", 0))  | 
|             SendEmail(ServerInfo)  | 
|             return  | 
|         elif stateType == "MapDisconnect":  | 
|             # µØÍ¼5·ÖÖÓδÏìÓ¦£¬¿ÉÒÔ¿¨ÁË»òÕßÉÁÍË ¹Ø±Õ  | 
|             #groupID=%s&userDBName=%s&Type=PyMongoError  | 
|             ServerInfo = "´íÎóÀàÐÍ:µØÍ¼±»¹Ø±Õ%s ·þÎñÆ÷×é:%s, dbname:%s"%(stateType,  | 
|                                                        dataDict.get("groupID", 0),  | 
|                                                        dataDict.get("userDBName", 0))  | 
|             SendEmail(ServerInfo)  | 
|             return    | 
|         elif stateType == "GameWarning":  | 
|             # ÓÎϷͨÓþ¯¸æÓʼþʹÓà  | 
|             ServerInfo = "ÓÎÏ·¾¯±¨:·þÎñÆ÷×é:%s, dbname:%s, ¾¯¸æÐÅÏ¢:%s"%(  | 
|                                                        dataDict.get("groupID", 0),  | 
|                                                        dataDict.get("userDBName", 0),  | 
|                                                        urllib.unquote_plus(dataDict.get("MsgInfo", 0)))  | 
|             SendEmail(ServerInfo)  | 
|         elif stateType in ["MapServerRaiseException", "GameServerRaiseException", "RaiseException"]:  | 
|             ServerInfo = "%s Å׳öTryÒì³£±¨´í:·þÎñÆ÷×é:%s, dbname:%s, MapID:%s Òì³£ÐÅÏ¢:%s"%(  | 
|                                                        stateType,  | 
|                                                        dataDict.get("groupID", 0),  | 
|                                                        dataDict.get("userDBName", 0),  | 
|                                                        dataDict.get("mapID", 0),  | 
|                                                        urllib.unquote_plus(dataDict.get("MsgInfo", 0)))  | 
|             SendEmail(ServerInfo)  | 
|         elif stateType == "QueryMapOK":  | 
|             if g_AllMapCnt == 0:  | 
|                 if not os.path.exists(".\MapCount.txt"):  | 
|                     return "===ÔÝÎÞ·´À¡£¬ÉԵȺó²éѯ==="  | 
|                 ifile = open(".\MapCount.txt")  | 
|                 text = ifile.read()  | 
|                 ifile.close()  | 
|                 return "=========ÉÏÒ»´Î¼Ç¼µÄ¿ªÆôÇé¿ö, Çë¹ý»áÔÙ²éѯ========", text  | 
|             return "·þÎñÆ÷¿ªÆôÍê±Ï£¬µØÍ¼Êý:%s"%g_AllMapCnt  | 
|     except Exception, e:  | 
|         print e  | 
|         ServerInfo = "¿ØÖÆÌ¨±¨´í£¬·þÎñÆ÷Òì³££¡  %s"%e  | 
|         try:  | 
|             SendEmail(ServerInfo)  | 
|         except:  | 
|             print "ÎÞ·¨·¢ËÍÓʼþ"  | 
|   | 
| # Óʼþ»ã±¨  | 
| def SendEmail(ServerInfo):  | 
|        | 
|     # µÚÈý·½ SMTP ·þÎñ  | 
|     mail_host="smtp.qq.com"  #ÉèÖ÷þÎñÆ÷  | 
|     mail_user="2199274165@qq.com"    #Óû§Ãû  | 
|     mail_pass="asmizpysxngtdjic"   # ¿ªÍ¨QQÓÊÏäµÄSMTP£¬¶ÌÐÅÑéÖ¤ºó»ñÈ¡µÄ£¬·ÇÃÜÂë  | 
|        | 
|        | 
|     sender = '2199274165@qq.com'  | 
|       | 
|     # ÕâÀï¿ÉÒÔÌîдÐèÒª½ÓÊջ㱨µÄÓʼþµØÖ·  | 
|     receivers = ['305670599@qq.com']#, '1142397645@qq.com', '305670599@qq.com', '995275190@qq.com']    | 
|        | 
|     message = MIMEText('Òì³£»ã±¨:%s'%ServerInfo, 'plain', 'gbk')  | 
|     message['From'] = Header("ÓÎÏ··þÎñÆ÷Òì³£»ã±¨", 'gbk')  | 
|     message['To'] =  Header("ÔËά", 'gbk')    # ²»·¢Ë͵ϰ»á±»¼Ç¼ΪÀ¬»øÓʼþ  | 
|        | 
|     subject = 'ÓÎÏ··þÎñÆ÷Òì³£'  | 
|     message['Subject'] = Header(subject, 'gbk')  | 
|        | 
|        | 
|   | 
|     smtpObj = smtplib.SMTP()   | 
|     smtpObj.connect(mail_host, 25)    # 25 Îª SMTP ¶Ë¿ÚºÅ£¬ Èç¹ûÓÃSSL ÐèÒª»»¶Ë¿Ú  | 
|     smtpObj.login(mail_user,mail_pass)    | 
|     smtpObj.sendmail(sender, receivers, message.as_string())  | 
|     print "Óʼþ·¢Ëͳɹ¦"  | 
|   |