Refactor: python: Move NextIP into its own function.
This is a good candidate to be a little utility function. I've improved
the comments and style somewhat so it's easier to see what's going on,
as well as removed its reliance on the environment and added a unit
test.
Also, raise ValueError if we run out of IP addresses instead of silently
making something invalid.