1 Star 0 Fork 0

AlimTech/wukong-contrib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SpeakIP.py 1.81 KB
一键复制 编辑 原始数据 按行查看 历史
# -*- coding: utf-8-*-
# 获取IP插件
import time
import socket
import subprocess
from robot import logging
from robot.sdk.AbstractPlugin import AbstractPlugin
logger = logging.getLogger(__name__)
class Plugin(AbstractPlugin):
SLUG = "speak_ip"
def getLocalIP(self):
ip = None
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('114.114.114.114', 0))
ip = s.getsockname()[0]
except:
name = socket.gethostname()
ip = socket.gethostbyname(name)
if ip.startswith("127."):
cmd = '''/sbin/ifconfig | grep "inet " | cut -d: -f2 | awk '{print $1}' | grep -v "^127."'''
a = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
a.wait()
out = a.communicate()
ip = out[0].strip().split("\n") # 所有的列表
if len(ip) == 1 and ip[0] == "" or len(ip) == 0:
return False
ip = '完毕'.join(ip)
return ip
def handle(self, text, parsed):
try:
count = 0
while True:
ip = self.getLocalIP()
logger.debug('getLocalIP: ', ip)
if ip == False:
self.say('正在获取中', cache=True)
else:
count += 1
ip += '完毕'
self.say(ip, cache=True)
if count == 1:
break
time.sleep(1)
except Exception as e:
logger.error(e)
self.say('抱歉,我没有获取到地址', cache=True)
def isValid(self, text, parsed):
return any(word in text for word in [u"IP", u"网络地址"])
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/alimtech/wukong-contrib.git
git@gitee.com:alimtech/wukong-contrib.git
alimtech
wukong-contrib
wukong-contrib
master

搜索帮助