如何在python中使用bash变量

2024-06-07 11:54:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在bash中,我可以在bash中执行此操作IP=$(wget -qO- ipinfo.io/ip)这将捕获我的公共IP并将其作为变量$IP存储在我的Raspberry Pi中。 现在我想在python中捕捉这个变量,当$IP不等于82.1x时,连接到gpio1的led亮起。xxx.xx号. 我在python方面是个新手,所以我需要一些帮助,我对python知之甚少,但对bash非常了解。如有任何帮助或建议,我们将不胜感激。在

提前谢谢你。在


Tags: ioipbashledpiwgetraspberry建议
1条回答
网友
1楼 · 发布于 2024-06-07 11:54:30

您应该使用os.environdict。请尝试:

>>> import os
>>> os.environ['IP'] 

或者

^{pr2}$

来自文件:

操作系统环境

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.

有奖游戏

您可以使用纯Python从http响应获取IP,如下所示:

import urllib2
# that's really cool!
IP = urllib2.urlopen("http://ipinfo.io/ip").read().strip()

相关问题 更多 >

    热门问题