detect_ip: avoid ResourceWarning about unclosed socket on python 3.x

i just added try/finally to support s.close(), rest is just block indentation change.

plus some typo / pep8 fixes.