I am confused by your while loop.
You are dividing by 1024, but network speed isn't in powers of 2, it's in powers of 10. So from bits, you divide by 10^6 to get Megabits per time. Or, you can divide bits by 10^9 to get Gigabits per time. I haven't done the backwards math to see if this picks...