【Python】【辅助程序】练手小程序:记录外网动态IP地址

 

练手小程序
程序作用:对IP实时记录;
1、定时获取外网IP,存储在本地文件中;
编写思路:
1)收集获取外网的API接口
2)定时执行
4)记录本地文件

 

 

 1 # -*- coding: utf-8 -*-
 2 # -*- coding: gbk -*-
 3 # Date: 2016/4/27
 4 # Created by zh
 5 
 6 from threading import Timer
 7 import time
 8 import urllib2
 9 import re
10 """
11 次数计数器
12 """
13 timecount = 0
14 timer_interval=1
15 """
16 记录时间
17 访问IP接口取出IP
18 写出 时间+ IP 到 IPcount+time.txt 文件中
19 """
20 def GetIP():
21     nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) 
22     response = urllib2.urlopen('https://ipip.yy.com/get_ip_info.php').read()
23     Currentip = re.search('\d+\.\d+\.\d+\.\d+',response).group(0)
24     f = open("IPcount+time.txt","a")
25     f.writelines(nowtime +" 当前的IP地址是 :" + Currentip +"\n")
26 t=Timer(timer_interval,GetIP)
27 t.start()
28 """  
29 循环执行代码
30 设定每隔一段时间取一次外网IP
31 """
32 while True:
33     time.sleep(5)
34     GetIP()
35     timecount += timer_interval
36 print ("program run %s time" % timecount )

 

转载于:https://www.cnblogs.com/17bdw/p/5460356.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值