我正在嵌入式linux系统上运行Python (2.7.2),其启动顺序大致如下:
0)重新启动
1)等待2分钟
2)通过DHCP获取网络配置。
守护进程使用尼普林获取当前时间并相应地更新其时钟。
import ntplib # http://pypi.python.org/pypi/ntplib/
...
self.ntpClient = ntplib.NTPClient()
...
def getDate(self):
try:
logging.info('Sending NTP request to %s' % ('pool.ntp.org'))
response = self.ntpClient.request('pool.ntp.org')
secs = time.localtime(response.tx_time)
logging.info('Response returned')
return secs
except Exception, e:
logging.exception(e)
return RESULT_FAILURE如果守护进程在dhcp运行之前开始解析地址信息,就会出现问题(步骤2)。这仍然是一个错误,即使在客户端运行之后也是如此。
这是一个例外:
ERROR:[Errno 2] temporary failure in name resolution.
Traceback (most recent call last):
File "/home/root/ntp_manager/ntp_manager.py", line 34, in getDate
response = self.ntpClient.request('pool.ntp.org')
File "/usr/lib/python2.7/ntplib.py", line 265, in request
addrinfo = socket.getaddrinfo(host, port)[0]
gaierror: [Errno 2] temporary failure in name resolution.但是,如果ntpClient在dhcp之后进行了第一次尝试,那么一切都正常。
这就好像错误是某种缓存(?!)的产物。
我的想法已经用完了,我会很感激你的帮助。
谢谢
发布于 2012-11-13 19:26:55
我已经想出了一个不需要任何其他库或脚本的解决方案。
在目录中的脚本中,当网络接口被打开时,将执行/etc/network/if-up.d/。这是一个参考文献。
因此,这确保了在系统配置了DHCP客户端之后,我的NTP脚本就会被执行,从而到达internet。这是避免我遇到的名称解析问题的正确行为。
我已经把这个简单的脚本(和chmod +x!)放在一起了!它检查网络方法是否为dhcp,并微妙地初始化我的NTP守护进程。
#! /bin/sh
# ntp time-sync manager python daemon starter script
set -e
# ... Some Stuff Omitted Here ...
if [ "$METHOD" = dhcp ]; then
echo -n "Starting $DESC..."
start-stop-daemon --start --verbose --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_ARGS
echo "METHOD: $METHOD"
echo "IFACE: $IFACE"
fi
echo "Exiting if-up.d/ntp_manager"
exit 0这里是系统上发生的事情的转储。请注意行:在IP被解析后立即执行run-parts /etc/network/if-up.d。
Reconfiguring network interfaces eth0 with DHCP... cat: can't open '/var/run/udh
cpc.eth0.pid': No such file or directory
run-parts /etc/network/if-pre-up.d
ifconfig eth0 up
udhcpc -R -b -p /var/run/udhcpc.eth0.pid -i eth0
udhcpc (v1.13.2) started
Sending discover...
Sending discover...
Sending select for 192.168.1.145...
Lease of 192.168.1.145 obtained, lease time 86400
adding dns 192.168.1.1
run-parts /etc/network/if-up.d
Starting NTP Time Manager...Daemon PID 1059
Redirecting serivice startup output to /home/root/Connect/log/ntp_manager_daemon.log
METHOD: dhcp
IFACE: eth0
Exiting if-up.d/ntp_manager无论如何,我认为这是一个简单而有力的解决方案。希望它能在未来帮助到其他人。
https://stackoverflow.com/questions/13318304
复制相似问题