| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1536 人关注过本帖
标题:获取adsl公网ip发送到邮箱原代码
只看楼主 加入收藏
hacbu84
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-9
收藏
 问题点数:0 回复次数:1 
获取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


搜索更多相关主题的帖子: 公网 adsl 代码 获取 
2010-08-09 11:41
KLML88422
Rank: 2
来 自:火星
等 级:论坛游民
威 望:1
帖 子:34
专家分:20
注 册:2010-11-12
收藏
得分:0 
恩不错!
2010-11-19 09:05
快速回复:获取adsl公网ip发送到邮箱原代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014881 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved