I suggest you look at the python requests library.
It has great support for basic http authentication out of the box.
e.g.
import requests
content = requests.get(URL, auth=('user', 'pass'))
Using requests
you can also set up sessions
(for cookie management) and easily POST
data (e.g. a login form) and keep the cookie to browse all the pages only accessible to logged in users.
Read more about session objects and posting data in the excellent documentation.
If you absolutely have to use urllib2
here's a useful snippet taken from another thread for basic HTTP authentication:
import urllib2, base64
request = urllib2.Request("http://api.foursquare.com/v1/user")
base64string = base64.standard_b64encode('%s:%s' % (username, password))
request.add_header("Authorization", "Basic %s" % base64string)
result = urllib2.urlopen(request)