如何使用python的一个bash变量 [英] How to use a bash variable in python
问题描述
在bash中我能够做到这一点在bash IP = $(wget的-qO- ipinfo.io/ip)
这抓住了我的公网IP,并将其存储为在我的树莓派变量$ IP。
现在,我想捕捉在Python这个变量,使连接到GPIO 1打开时, $ IP
不等于82.1x.xxx.xx一个领导。
我是那种在python,所以我需要一些帮助的新手,我有蟒蛇知之甚少,但在bash非常良好的知识。任何帮助或建议将AP preciated。
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.
感谢您提前。
推荐答案
您应该使用的 os.environ 字典。试试看:
You should use os.environ dict. Try it out:
>>> import os
>>> os.environ['IP']
或
>>> os.environ.get('IP')
从DOC:
os.environ
一个映射对象再presenting字符串环境。例如,
ENVIRON [家]是你的主目录的路径名(在某些
平台),和相当于GETENV(HOME)用C
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.
奖金游戏
您可以从HTTP响应只使用纯Python,像这样抢IP:
You could grab IP from http response just using pure Python, something like this:
import urllib2
# that's really cool!
IP = urllib2.urlopen("http://ipinfo.io/ip").read().strip()
这篇关于如何使用python的一个bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!