看了下python下的logging模块,和java的log4j差不多,把之前的代码改为使用log配置的方式实现功能(需求和之前的相同,地址"http://www.cnblogs.com/GYoungBean/p/6510508.html")
#! usr/bin/env python3
# -*- coding:utf-8 -*- import socket
import threading
import logging.config now_ip = "" def _get_ip_win():
ips = socket.gethostbyname_ex(socket.gethostname())
for s in ips[2]:
if s.startswith(''):
return s def checkIP(start_ip):
global now_ip
if start_ip != now_ip:
logger_mail.error('Send Mail:the old IP is:%s.Now,the new IP is:%s' % (now_ip,start_ip))
now_ip = start_ip
else:
logger_root.info('IP address not change,the old IP is:%s---the new IP is:%s' % (start_ip, now_ip))
new_ip = _get_ip_win()
t = threading.Timer(3600.0, checkIP,(new_ip,))
t.start() if __name__ == '__main__':
logging.config.fileConfig("./logging.conf")
logger_mail = logging.getLogger('mail')
logger_root = logging.getLogger('root')
start_ip,now_ip = _get_ip_win(),_get_ip_win()
checkIP(start_ip)
日志配置如下(logging.conf):
[loggers]
keys=root,mail [handlers]
keys=consoleHandler,fileHandler,mailHandler [formatters]
keys=simpleFormatter [formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s - [%(filename)s:%(lineno)s]
datefmt= [logger_root]
level=INFO
handlers=consoleHandler,fileHandler [logger_mail]
level=ERROR
handlers=mailHandler,fileHandler
qualname=mail
propagate=0 [handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,) [handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormatter
args=('./ipchange.log', 'a') [handler_mailHandler]
class=handlers.SMTPHandler
level=ERROR
formatter=simpleFormatter
args=(('xxx',25), 'xxx', ['xxx','xxx'], 'NOTICE!!!--PC IP CHANGED', ('xxx', 'xxx'))

python3练习-发送IP地址到邮箱(使用日志)的更多相关文章

  1. python3练习-发送IP地址到邮箱

    需求: 从外网通过VPN访问内网,并登录电脑A.在电脑A通过共享文件夹(需要\\IP的方式访问)访问到内网电脑B,由于电脑B的WI-FI的IP地址会变化,所以当电脑B的I访问路径需要获知电脑B的最新I ...

  2. 树莓派开机发送IP地址到邮箱

    树莓派使用的wifi联网,在宿舍使用的是公共网络,不能设置静态ip,每次树莓派上电开机后ip地址可能会改变,所以让树莓派开机联网后自动发送ip地址到QQ邮箱 一.安装mutt和msmtp mutt: ...

  3. 让树莓派自动上报IP地址到邮箱,二代B

    由于我使用树莓派的场景大多数是在没有显示器.只用terminal连接它的情况下,所以,它的IP地址有时会在重启之后变掉(DHCP的),导致我无法通过terminal连接上它.然后我又要很麻烦地登录路由 ...

  4. 树莓派获取ip地址发送到邮箱

    公网 ip.sh curl http://members.3322.org/dyndns/getip >>/email/ip.log python /email/mail.py ##### ...

  5. C#服务器获取客户端IP地址以及归属地探秘

    背景:博主本是一位Windows桌面应用程序开发工程师,对网络通信一知半解.一日老婆逛完某宝,问:"为什么他们知道我的地址呢,他们是怎么获取我的地址的呢?" 顺着这个问题我们的探秘 ...

  6. OpenStack中虚拟机获取不到IP地址的解决方法

    OpenStack源码交流群: 538850354 系统环境: centos6.5 + icehouse多节点部署 问题描述: 使用测试镜像cirros,虚拟机实例可以正常启动,但是不能从IP池中获取 ...

  7. 常用正则表达式大全,手机、电话、邮箱、身份证(最严格的验证)、IP地址、网址、日期等

    <script type="text/javascript">/* * 手机号码格式 * 只允许以13.15.18开头的号码 * 如:13012345678.15929 ...

  8. php使用过滤器filter_var轻松验证邮箱url和ip地址等

    以前使用php的时候还不知道有过滤器filter这玩意,那时候判断邮箱.url和ip地址格式是否符合都是用正则表达式.后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来 ...

  9. 验证中文、英文、电话、手机、邮箱、数字、数字和字母、Url地址和Ip地址的正则表达式

    Helper类代码 public class Helper { #region 单列循环 private Helper() { } private static Helper instance = n ...

随机推荐

  1. mysql 远程登录

    mysql -h 192.168.5.116 -P 3306 -u root -p123456

  2. android 前台服务不显示通知

    原因可以在哪里写了执行完成后就自动结束的吧 导致前台服务没有出现 如我 @Override public int onStartCommand(Intent intent, int flags, in ...

  3. CSRF与JSON

    之前遇到提交json的请求想要进行csrf攻击都是用的闭合表单的方法,很笨很麻烦, 这次看到了别人的操作记录一下. 这里用到了ajax异步请求(但是这里我有个疑问就是:这里用到了cors跨域,是不是必 ...

  4. linux----------安装Supervisor是用Python开发的一套通用的进程管理程序

    1.linux环境必须安装 python 2.yum install python-setuptools 3.获取supervisor包 wget https://pypi.python.org/pa ...

  5. PHP日历的算法

    <?php if (function_exists('date_default_timezone_set')) { date_default_timezone_set('Asia/Chongqi ...

  6. Python 两个星号(**)的 参数

    将参数以字典的形式导入

  7. 用 python 生成一个简单的词云图

    import jieba from nltk import * from wordcloud import WordCloud import matplotlib.pyplot as plt word ...

  8. vue axios使用form-data的形式提交数据的问题

    vue axios使用form-data的形式提交数据vue axios request payload form data由于axios默认发送数据时,数据格式是Request Payload,而并 ...

  9. Python手势识别

    这是借鉴了github上的一个源程序,参考源:https://github.com/lzane/Fingers-Detection-using-OpenCV-and-Python 自己在这个基础上做了 ...

  10. jQuery 查找元素1

    jQuery 查找元素1 1. id // 通过id查找 $('#id') 2. class <div class='c1'></div> // 通过class查找 $('.c ...