Attribute Error: ‘type’ object has no attribute ‘datetime’

While trying to write some test for NTP servers, my response time used this:
I am using IronPython 2.7.

timetoreturn = time.time()            
if(self.response_time != 0):                
timetoreturn = (self.response_time – datetime.datetime(1970,1,1)).total_seconds()

I got this error: Attribute Error: ‘type’ object has no attribute ‘datetime’.

My imports were:
import datetime
from datetime import datetime, tzinfo

Come to find out, using import datetime just causes problems so I removed that. That left me with ‘from datetime import datetime, tzinfo’.

Now. What the heck is the deal?

This is what. Datetime module has an object named datetime that you can use. Which is very confusing. So FROM DATETIME means that I am in the datetime module, importing datetime object to use, and from there I can just use the attributes. Instead, I was writing ‘datetime.datetime’ meaning I was already in the second level of inception (datetime > datetime) and then trying to access an attribute named ‘datetime’. So datetime.datetime.datetime.

This doesn’t work. 🙂

Just be aware of the level of the module you are in when importing and accessing attributes.

Python: Pinging a Server Using Windows

I found a snippet online at StackOverflow:

https://stackoverflow.com/questions/2953462/pinging-servers-in-python/32684938

I am working with two FTP servers at work and am writing tests for the application we run on it. The test case I am handling right now involves having no application, no backup, and no valid application on the FTP server that will FTP it to the machine. I get an Updater FTP error. After 1s, 2s, 3s, 1m, 10m, and 1 hour it will reboot the machine and since I only have it in the log files to prove that, I need to have the test know when this reboot happens. So I am pinging the machine once a sec for 4,266 seconds and when it goes offline, it will return false.

I encountered a problem I want to document for the future in case anyone else or myself forgets this will happen.

On any other machine, a ‘Destination Host Unreachable’ error will be False, but if you specify identifying the machine as Windows, it will still return true.

Python logic to tell when the ping ends.
Python method to ping a server.