在bash中,我可以在bash中执行此操作IP=$(wget -qO- ipinfo.io/ip)
这将捕获我的公有IP并将其作为variables$ IP存储在我的Raspberry Pi上。 现在我想在python中捕获这个variables,使得当$IP
不等于82.1x.xxx.xx时,连接到GPIO 1的led灯亮。 我是一个Python新手,所以我需要一些帮助,我有很less的知识在Python中,但在bash非常好的知识。 任何帮助或build议将不胜感激。
先谢谢你。
你应该使用os.environ字典。 试试看:
>>> import os >>> os.environ['IP']
要么
>>> os.environ.get('IP')
从doc:
os.environ
表示字符串环境的映射对象。 例如,environ ['HOME']是您的主目录(在某些平台上)的路径名,相当于C中的getenv(“HOME”)。
奖金游戏
您可以使用纯Python从http响应中获取IP,如下所示:
import urllib2 # that's really cool! IP = urllib2.urlopen("http://ipinfo.io/ip").read().strip()