在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
首先列出Go语言版的,通过这个小工具对比了下,go的开发效率与python差不多,稍比python多了些代码。
package main import ( "encoding/json" "fmt" "io/ioutil" "net/http" "net/smtp" "reflect" "strconv" "strings" "time" ) //报警接收email var emails = [...]string{"[email protected]", "[email protected]"} //-------------------------------------------------------------------------------- //邮件发送函数 func SendMail(user, password, host, to, subject, body, mailtype string) error { hp := strings.Split(host, ":") auth := smtp.PlainAuth("", user, password, hp[0]) var content_type string if mailtype == "html" { content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8" } else { content_type = "Content-Type: text/plain" + "; charset=UTF-8" } msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body) fmt.Println(string(msg)) send_to := strings.Split(to, ";") err := smtp.SendMail(host, auth, user, send_to, msg) return err } //-------------------------------------------------------------------------------- //用户注册检查 func Check_register(platform_url, username, password string) (err error, ret string) { url := fmt.Sprintf("http://%s/api/register/?username=%s&password=%s", platform_url, username, password) response, err := http.Get(url) defer response.Body.Close() body, _ := ioutil.ReadAll(response.Body) return err, string(body) } //-------------------------------------------------------------------------------- //用户登陆检查 func Check_login(platform_url, username, password, login_server string) (err error, ret string) { url := fmt.Sprintf("http://%s/api/login/?username=%s&password=%s&login_server=%s&port=5051", platform_url, username, password, login_server) response, err := http.Get(url) defer response.Body.Close() body, _ := ioutil.ReadAll(response.Body) return err, string(body) } //-------------------------------------------------------------------------------- //主函数 func main() { username := strconv.Itoa(int(time.Now().Unix())) password := username platform_url := "www.xxxgame.com" login_server := "123.123.123.123" mail_host := "smtp.qq.com:25" mail_user := "[email protected]" mail_pass := "1234567890" url := fmt.Sprintf("http://%s/api/register/?username=%s&password=%s", platform_url, username, password) fmt.Println(url) //检查用户注册 subject := fmt.Sprintf("X项目服务器[%s]注册用户成功", login_server) content := fmt.Sprintf("注册用户成功, 服务器IP:%s, 用户名:%s, 密码:%s", login_server, username, password) err, ret := Check_register(platform_url, username, password) var reg interface{} err = json.Unmarshal([]byte(ret), ®) fmt.Println(err, reg) fmt.Println(reflect.TypeOf(reg)) m := reg.(map[string]interface{}) result := false for _, v := range m { switch vv := v.(type) { case bool: result = vv default: } } fmt.Println(result) if err == nil && result { for _, email := range emails { fmt.Println(email) fmt.Println(SendMail(mail_user, mail_pass, mail_host, email, subject, content, "txt")) } } //检查用户登陆 subject = fmt.Sprintf("X项目服务器[%s]用户登陆成功", login_server) content = fmt.Sprintf("用户登陆成功, 服务器IP:%s, 用户名:%s, 密码:%s", login_server, username, password) err, ret = Check_login(platform_url, username, password, login_server) var log interface{} err = json.Unmarshal([]byte(ret), &log) fmt.Println(err, log) fmt.Println(reflect.TypeOf(reg)) m = reg.(map[string]interface{}) result = false for _, v := range m { switch vv := v.(type) { case bool: result = vv default: } } fmt.Println(result) if err == nil && result { for _, email := range emails { fmt.Println(email) fmt.Println(SendMail(mail_user, mail_pass, mail_host, email, subject, content, "txt")) } } }
python版的: # -*- coding: utf-8 -*- import urllib2 import time import json import smtplib from email.mime.text import MIMEText #要发给谁 mail_to=['[email protected]', '[email protected]'] #------------------------------------------------------------------------------- #邮件发送函数(收件人,标题,内容) def send_mail(to_list, sub, content): #设置服务器,用户名、口令以及邮箱的后缀 mail_host="smtp.qq.com" mail_user="1234567890" mail_pass="1234567890" mail_postfix="qq.com" me=mail_user+"<"+mail_user+"@"+mail_postfix+">" msg = MIMEText(content) msg['Subject'] = sub msg['From'] = me msg['To'] = to_list try: s = smtplib.SMTP() s.connect(mail_host) s.login(mail_user,mail_pass) s.sendmail(me, to_list, msg.as_string()) s.close() except Exception, e: print e #------------------------------------------------------------------------------- #获取指定url的内容 def gethtml(url): req = urllib2.Request(url) response = urllib2.urlopen(req) html = response.read() return html #-------------------------------------------------------------------------------- #检查注册用户 def check_register(platform_url, username,password): url = "http://%s/api/register/?username=%s&password=%s" % (platform_url, username, password) ret = gethtml(url) return ret #-------------------------------------------------------------------------------- #检查用户登陆 def check_login(platform_url, username,password, login_server): url = "http://%s/api/login/?username=%s&password=%s&login_server=%s&port=5051" % (platform_url, username, password, login_server) ret = gethtml(url) return ret #-------------------------------------------------------------------------------- #主函数 if __name__ == '__main__': username = str(time.time())[:10] password = "1234567890" platform_url = "www.xxxgame.com" login_server = "123.123.123.123" #检查注册用户 print "check_register" ret = check_register(platform_url, username,password) ret = eval(str(json.loads(ret))) result = ret.get('result') info = ret.get('error_info') print result, info sub = 'X项目服务器[%s]注册用户成功' % login_server content = '注册用户成功, 服务器IP:%s, 用户名:%s, 密码:%s' % (login_server, username, password) if result: for mail in mail_to: send_mail(mail, sub, content) #检查用户登陆 print "check_login" ret = check_login(platform_url, username,password, login_server) ret = eval(str(json.loads(ret))) print ret result = ret.get('result') info = ret.get('error_info') print result, info sub = 'X项目服务器[%s]登陆用户成功' % login_server content = '用户登陆成功, 服务器IP:%s, 用户名:%s, 密码:%s' % (login_server, username, password) if result: for mail in mail_to: send_mail(mail, sub, content)
|
请发表评论