• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

监控服务器的注册及登陆并邮件通知的代码(go/python)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先列出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), &reg)
    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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
client-go 和 golang 源码中的技巧发布时间:2022-07-10
下一篇:
(♂)程序打包工具setup2go使用教程 - Xt Idt发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap