如果脚本启动后连接了电缆,Python无法获得IP [英] Python not getting IP if cable connected after script has started
问题描述
我希望这不会进入超级用户领域.
I hope this doesn't cross into superuser territory.
所以我有一个嵌入式linux,系统进程自然很简单.我不太确定哪个系统进程会监视物理层并在插入网络电缆时启动dhcp客户端,但是我自己做了一个. ¨ 问题是,如果我有一个使用http连接的python脚本,并且在没有IP地址之前运行,它将永远无法建立连接.即使在我拥有有效的IP之后,python仍然具有
So I have an embedded linux, where system processes are naturally quite stripped. I'm not quite sure which system process monitors to physical layer and starts a dhcp client when network cable is plugged in, but i made one myself. ¨ The problem is, that if i have a python script, using http connections, running before i have an IP address, it will never get a connection. Even after i have a valid IP, the python still has
名称解析中的临时错误"
"Temporary error in name resolution"
那么,如何在不重新启动脚本的情况下使python实现新的可用连接呢?
So how can I get the python to realize the new connection available, without restarting the script?
或者,我是否缺少一些正常的程序,Linux在网络电缆连接上正常运行.
Alternatively , am I missing some normal procedure Linux runs normally at network cable connect.
我正在使用的dhcp客户端是udhcpc,而python版本是2.6.使用httplib进行连接.
The dhcp client I am using is udhcpc and python version is 2.6. Using httplib for connections.
推荐答案
类似httplib的声音/etc/resolv.conf
会在您的DHCP客户端获得IP后更新.
Sounds like httplib caches /etc/resolv.conf
which is updated after your DHCP client obtains an IP.
您可能会考虑编写一个包装器脚本,该脚本将等到获得IP地址后再调用您的python脚本.
You might consider writing a wrapper script that waits until an IP address is obtained, then calls your python script.
或者,您可以尝试在python脚本中不打开任何套接字连接,直到获得IP地址并更新/etc/resolv.conf
.
Alternatively, you could try to, within your python script, not open any socket connections until you have acquired an IP address and /etc/resolv.conf
updated.
修改
httplib使用socket.create_connection()
,经过一些搜索,我发现做缓存 /etc/resolv.conf
. (好吧,看来libc的嵌入式版本实际上是在进行缓存).
httplib uses socket.create_connection()
which, after some searching, I found does cache /etc/resolv.conf
. (Well, it seems that the embedded version of libc is actually doing the caching).
您可以尝试 SugarLabs的解决方案,尽管我可以没说这是有效的.
You could try SugarLabs' solution, although I can't speak to it's effectiveness.
这篇关于如果脚本启动后连接了电缆,Python无法获得IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!