forked from ma6174/speak_raspi_ip
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspeak_ip.py
73 lines (62 loc) · 1.61 KB
/
speak_ip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# coding=utf-8
import os
import sys
import time
import socket
import subprocess
voice_path = os.path.join(sys.path[0], 'voice')
player = "mpg123"
def getLocalIP():
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 getFilePath(filename):
return os.path.join(voice_path, "%s.mp3" % filename)
def play(voice):
cmd = "%s %s" % (player, getFilePath(voice))
a = subprocess.Popen(
cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
a.wait()
def speak(ip):
for i in ip:
if i == ".":
play("点")
else:
play(i)
play("完")
if __name__ == '__main__':
count = 0
while True:
ip = getLocalIP()
print ip
if ip == False:
play("正在获取网络地址")
else:
count += 1
speak(ip)
if count == 10:
break
time.sleep(1)