[Python] Creating Custom Cookies for HTTP Requests

One way of adding a custom cookie is to directly specify the "cookie" field in your request header. For example:

request.add_header("Cookie", "something=test+1+2+3; source=script")

Here, two cookies are set in the request ("something" and "source"). Notice that the cookies are fully specified in the string, instead of using urllib.urlencode. Another way of adding cookies is to create Cookie objects and add them to your CookieJar. Before sending your request, add the cookies by using add_cookie_header.
Read more of this post

[Python] Using Basic HTTP Authentication

To use urllib2 to access content that requires HTTP authentication, you need to create an OpenerDirector with a HTTPBasicAuthHandler. With HTTPBasicAuthHandler, add the authentication details by calling its add_password method. It takes in four parameters – realm, uri, user and passwd.
Read more of this post