Python not getting IP if cable connected after script has started

冷暖自知 提交于 2019-12-01 11:52:35

问题


I hope this doesn't cross into superuser territory.

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"

So how can I get the python to realize the new connection available, without restarting the script?

Alternatively , am I missing some normal procedure Linux runs normally at network cable connect.

The dhcp client I am using is udhcpc and python version is 2.6. Using httplib for connections.


回答1:


Sounds like httplib caches /etc/resolv.conf which is updated after your DHCP client obtains an IP.

You might consider writing a wrapper script that waits until an IP address is obtained, then calls your python script.

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.

Edit

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).

You could try SugarLabs' solution, although I can't speak to it's effectiveness.




回答2:


it might be that one of the python modules is caching the state of the network, and so it isn't using the latest settings. Try and reload all the network related modules. A simple example of this is:

import sys, socket, urllib

for i in [sys, socket, urllib]:
    reload(i)

if that doesn't work look around in sys.modules to see what else got imported, and try more modules there. A more extreme version of the reloading would be the code below, that you could try as a last ditch effort.

code

import sys
print 'reloading %s modules ' % len(sys.modules)
for name, module in sys.modules.items():
    try:
        reload(module)
    except:
        print 'failed to import %s' % name

output

reloading 42 modules 
failed to import __main__
failed to import encodings.encodings
failed to import encodings.codecs
failed to import lazr
failed to import encodings.__builtin__



回答3:


After alot more research, the glibc problem jedwards suggested, seemed to be the problem. I did not find a solution, but made workaround for my usecase. Considering I only use one URL, I added my own "resolv.file" . A small daemon gets the IP address of the URL when PHY reports cable connected. This IP is saved to "my own resolv.conf". From this file the python script retrieves the IP to use for posts.

Not really a good solution, but a solution.



来源:https://stackoverflow.com/questions/13606584/python-not-getting-ip-if-cable-connected-after-script-has-started

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!