获取adsl公网ip发送到邮箱原代码
原脚本:
#coding=gbk
import os
import re
import urllib2
import smtplib
import time
from time import localtime,strftime
#################参数设置######################
#邮箱用户名
username = "#####"
#邮箱密码
password = "******"
#smtp服务器
mailserver = "smtp.
#smtp服务器端口
port = "25"
#执行间隔,单位秒
interval = 300
#发件地址
from_addr = "#####@
#收件地址
to_addr = "#####@
#是否打开debug(0关闭,1打开)
debuglevel = 0
################################################
running = True
hostname = os.environ['COMPUTERNAME']
lastip = "0.0.0.0"
while running:
#获得公网IP
pagereturn = urllib2.urlopen("http://www.).read()
#print pagereturn
#截取IP地址
obj = re.search('\d+\.\d+\.\d+\.\d+', pagereturn)
ip = obj.group(0)
print "程序运行中,主机当前广域网IP为:" + ip
#比较当前IP与之前IP是否相同,如不同则发送邮件
if ip <> lastip:
lastip = ip
now = strftime("%Y-%m-%d %H:%M:%S", localtime())
subject = now + " ip主题"
content = now + " 主机: " + hostname + " 当前IP地址为: " + ip
smtp = smtplib.SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect(mailserver, port)
smtp.login(username, password)
smtp.sendmail(from_addr, to_addr, 'From: ' + from_addr + '\r\nTo: ' + to_addr +
'\r\nSubject: ' + subject + '\r\n\r\n' + content)
smtp.quit()
time.sleep(interval)
在xp下安装2.6.5版本,运行正常,在win7下面cmd下提示如下
在xp下安装2.7版本,运行也出现如下提示
请那位给出解答,谢谢
如何能让上面的脚本,在win系统下面一直运行检测?ip一旦发生变化就会发送邮件
我做过以下测试,开机自启,但没有进行宽带连接,一两个钟后,才宽带连接,但是,py脚本已经停止工作
我要怎么办呢?
运行sendip.py脚本出现如下
File "D:\python\sendip.py", line 34, in <module>
pagereturn = urllib2.urlopen("http://www.).read()
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 397, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 510, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 435, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 518, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found