Python not getting IP if cable connected after script has started

后端 未结 3 1670
醉酒成梦
醉酒成梦 2021-01-16 14:51

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 syst

3条回答
  •  醉话见心
    2021-01-16 15:54

    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__
    

提交回复
热议问题