Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 用Python查找公共IP的更好方法?_Regex_Python 2.7_Beautifulsoup - Fatal编程技术网

Regex 用Python查找公共IP的更好方法?

Regex 用Python查找公共IP的更好方法?,regex,python-2.7,beautifulsoup,Regex,Python 2.7,Beautifulsoup,所以,我对Python还是相当陌生的。话虽如此,我一直在Coursera上Chucks博士的可爱课程。当然,这让我开始尝试编写一个程序来解决我一直遇到的问题 我想创建一个程序,它将获取我的公共IP地址,对照我存储的内容进行检查,并在更改时向我发送更新(非静态公共IP) 到目前为止,我已经想出了这个方法来处理IP检查(在这个网站和其他网站的帮助下…)。它包括漂亮的汤来帮助解析HTML和正则表达式,以帮助从剩下的内容中找到IP地址 这个代码可以工作,但是把它撕碎!有什么能更好地工作,或者更“pyth

所以,我对Python还是相当陌生的。话虽如此,我一直在Coursera上Chucks博士的可爱课程。当然,这让我开始尝试编写一个程序来解决我一直遇到的问题

我想创建一个程序,它将获取我的公共IP地址,对照我存储的内容进行检查,并在更改时向我发送更新(非静态公共IP)

到目前为止,我已经想出了这个方法来处理IP检查(在这个网站和其他网站的帮助下…)。它包括漂亮的汤来帮助解析HTML和正则表达式,以帮助从剩下的内容中找到IP地址

这个代码可以工作,但是把它撕碎!有什么能更好地工作,或者更“pythonic”?是的,我想把Chuck博士的一些经验融入其中,所以也许不需要美味的汤。也许有一种方法可以用更少的代码来实现这一点?我是个笨蛋,所以温柔点

import urllib
import ssl
from BeautifulSoup import *
import re

try:
    url = "https://www.ipchicken.com"

    scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    uh = urllib.urlopen(url, context=scontext)
    data = uh.read()
    soup = BeautifulSoup(data)
    tags = soup('p')

x = tags[1].text

ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', x)
except:
    print "No network access detected, check connection"
    exit()
print ip[0]
myip = ip[0]
try:
    fhand = open('myip.txt')
    oldip = fhand.read()
except:
    fhand = open('myip.txt', 'w')
    fhand.write(myip)
    fhand.close()
    print "New IP file created."
    exit()
if oldip == myip:
    print "Current IP still matches"
    exit()
if oldip != myip:
    print "IP has CHANGED"
    fhand = open('myip.txt', 'w')
    fhand.write(myip)
    fhand.close()
    exit()
这是在Windows10机器上编写的,因此它可能无法在Linux(Raspbian)中创建新文件,我最终将把它传输到Linux。我得试一试,看看是否管用。我想把它放到Chron目录中,每15分钟运行一次

更新: 我找到了通知我变化的机会。新守则是:

    import urllib
    import ssl
    from BeautifulSoup import *
    import re
    import smtplib
    try:
        url = "https://www.ipchicken.com"

        scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
        uh = urllib.urlopen(url, context=scontext)
        data = uh.read()
        soup = BeautifulSoup(data)
        tags = soup('p')

        x = tags[1].text

        ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', x)
    except:
        print "No network access detected, check connection"
        exit()
    print ip[0]
    myip = ip[0]
    try:
        fhand = open('myip.txt')
        oldip = fhand.read()
    except:
        fhand = open('myip.txt', 'w')
        fhand.write(myip)
        fhand.close()
        print "New IP file created."
        exit()
    if oldip == myip:
        print "Current IP still matches"
        exit()
    if oldip != myip:
        print "IP has CHANGED"
        fhand = open('myip.txt', 'w')
        fhand.write(myip)
        fhand.close()

        user = 'youremail@gmail.com'
        password = 'yourpassword'

        recipients = ['yourcellnumber@vtext.com']
        sender = 'youremail@gmail.com'
        message = myip

        session = smtplib.SMTP_SSL('smtp.gmail.com')
        session.login(user, password)
        session.sendmail(sender, recipients, message)
        session.close()
        exit()

现在,代码将通过gmail以文本形式发送一封电子邮件到我的手机上(在我的例子中是Verizon)。Gmail也很好,当您查看门户网站时,它会将此信息显示为已发送的消息。发送文本似乎需要5-10分钟,但确实有效。我还注意到,如果这段代码试图在不到5分钟的时间内发送电子邮件,gmail就会出错。这应该不是问题,因为IP不会经常改变。是的,您必须通过web浏览器(gmail.com)授权不太安全的应用程序才能运行。

您可以使用icanhazip.com服务。它只提供您的公共IP,而不需要您使用bs4将其过滤掉

所以总体代码应该是:

import requests

def getPublicIP():
    r = requests.get("http://icanhazip.com")
    return r.content().strip()
请求模块不在标准库中,请参见以下内容
链接:

我还没有对你的大部分代码发表评论,但是应该没有必要查询远程网站来获取你的IP(除非你在路由器后面,你想要它的IP)。要获取本地系统的IP地址,可以使用
导入套接字;my_ip=socket.gethostbyname(socket.gethostname())