You should probably use the HTTPBasicAuthHandler from urllib2 that will do this work for you.
You can have a look on how to do that here:
http://docs.python.org/2/library/urllib2.html#examples