Learn Python

From UEQ Wiki
Jump to: navigation, search

Home (uequations.com)   >>   UEQ Wiki TOC

Notes

def create_url_opener(url, username, password):

"""

Initializes a password manager for APIs and creates

URL opener with authentication.

 :param url:
 :return: opener to be installed.
"""
try:

headers = [('Accept', '*/*')]

auth_code = base64.b64encode(username + ':' + password)
logging.debug(auth_code)
headers.append(('Authorization', 'Basic ' + auth_code))
opener = urllib2.build_opener()
opener.addheaders = headers

return opener

except urllib2.HTTPError as error:

errorMessageJson = encodeHTTPErrorToJson(url, error)

processedErrorMessageToJson = process(errorMessageJson)
errorJsonResponse = finalizeJson(processedErrorMessageToJson)
print(errorJsonResponse)

sys.exit(1)

Useful Links