I have a laptop which has a static IP (193.2.1.8)and is part of a small local private network without internet access. Quite often, I am required to do the following:
I would like to simply this process. Thus thinking of creating a Python Script that will
Thanks to a post on SO which suggested to use WMI
module, I managed to change the IP configuration using WMI
module like this:
import wmi
nic_configs=wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)
nic = nic_configs[0]
nic.EnableDHCP()
But my question now is, how do I know if I am safe to proceed with my FTP operation? How do I know that if the internet connection is live after I have enabled the DHCP?
For the FTP operation, I am thinking of using ftplib
.
Is there any better approach to this?
FYI: I am on windows XP SP3 32bits, python 2.7.5
how do I know if I am safe to proceed with my FTP operation?
The first step in using ftplib is to create an FTP instance. If you give it a host name, the method call connect(host)
is made for you. If the network is unreachable, it will fail in it's connection attempt and an exception is raised. There is also a timeout option to set connection timeout.
If the connection attempt is successful, your network adapter was obviously ready. If an exception was raised, it was not. You can build your logic for connecting based on that (ideally using a retry decorator, or even a simple loop with a small sleep between each attempt).