from datetime import datetime
dt, micro = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f').split('.')
dt = '%s.%03d' % (dt, int(micro) / 1000)
print(dt)
Format a datetime into a string with milliseconds