fixed a missing finally clause and moved urllib import to the top.