In bash I'm able to do this in bashIP=$(wget -qO- ipinfo.io/ip)
This captures my public IP and stores it as the variable $IP on my Raspberry Pi. Now I want to capture this variable in python to make a led connected to GPIO 1 turn on when the $IP
is not equal to 82.1x.xxx.xx . I'm kind of a newbie in python so I need some help, I have very little knowledge in python but very good knowledge in bash. Any help or suggestions will be appreciated.
在bash中,我能够在bashIP = $(wget -qO- ipinfo.io/ip)中执行此操作。这将捕获我的公共IP并将其作为变量$ IP存储在我的Raspberry Pi上。现在我想在python中捕获这个变量,当$ IP不等于82.1x.xxx.xx时,使一个连接到GPIO 1的led开启。我是python中的新手所以我需要一些帮助,我对python知之甚少,但对bash知之甚少。任何帮助或建议将不胜感激。
Thank you in advance.
先感谢您。
1 个解决方案
#1
You should use os.environ dict. Try it out:
你应该使用os.environ dict。试试看:
>>> import os
>>> os.environ['IP']
or
>>> os.environ.get('IP')
From doc:
os.environ
A mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
表示字符串环境的映射对象。例如,environ ['HOME']是主目录的路径名(在某些平台上),相当于C中的getenv(“HOME”)。
BONUS GAME
You could grab IP from http response just using pure Python, something like this:
您可以使用纯Python从http响应中获取IP,如下所示:
import urllib2
# that's really cool!
IP = urllib2.urlopen("http://ipinfo.io/ip").read().strip()
#1
You should use os.environ dict. Try it out:
你应该使用os.environ dict。试试看:
>>> import os
>>> os.environ['IP']
or
>>> os.environ.get('IP')
From doc:
os.environ
A mapping object representing the string environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.
表示字符串环境的映射对象。例如,environ ['HOME']是主目录的路径名(在某些平台上),相当于C中的getenv(“HOME”)。
BONUS GAME
You could grab IP from http response just using pure Python, something like this:
您可以使用纯Python从http响应中获取IP,如下所示:
import urllib2
# that's really cool!
IP = urllib2.urlopen("http://ipinfo.io/ip").read().strip()