Python

Python is a programming language that you can use to manage your Akamai services.

Install Python

Download the Python release compatible with your operating system at https://www.python.org/downloads/ and install it.

Authenticate

In order to complete these steps, you need to first Create authentication credentials.

Follow these steps to use the python-edgegrid authentication handler.

  1. Install the EdgeGrid authentication for Python.
    pip install edgegrid-python

  2. Edit this code with the variables from your credentials file.
    You'll need the values for the tokens from your .edgerc file.

    import requests
    from akamai.edgegrid import EdgeGridAuth
    from urllib.parse import urljoin
    
    baseurl = 'https://akaa-WWWWWWWWWWWW.luna.akamaiapis.net/' # this is the "host" value from your credentials file
    s = requests.Session()
    s.auth = EdgeGridAuth(
        client_token='ccccccccccccccccc',
        client_secret='ssssssssssssssssss',
        access_token='aaaaaaaaaaaaaaaaaaaaa'
    )
    
    result = s.get(urljoin(baseurl, '/contract-api/v1/contracts/identifiers'))
    print(result.status_code)
    print(result.json())
    
  3. Run your code.

python path/to/your_code_file.py

You'll see a response like this:

200
       ['P-3HHAM38']

Example API call:

>>> import requests
>>> from akamai.edgegrid import EdgeGridAuth, EdgeRc
>>> from urllib.parse import urljoin

>>> edgerc = EdgeRc('~/.edgerc')
>>> section = 'default'
>>> baseurl = 'https://%s' % edgerc.get(section, 'host')

>>> s = requests.Session()
>>> s.auth = EdgeGridAuth.from_edgerc(edgerc, section)

>>> result = s.get(urljoin(baseurl, '/diagnostic-tools/v2/ghost-locations/available'))
>>> result.status_code
200
>>> result.json()['locations'][0]['value']
Oakbrook, IL, United States
...

This is an example of an API call to List available edge server locations. Change the baseurl element to reference an endpoint in any of the Akamai APIs.

📘

If your .edgrc file has more than one credential set separated with a [header], use the section = argument to specify which section from the configuration file contains the desired credentials for your API request.

Access the GitHub repository at EdgeGrid for Python.

Set up a Virtual Environment

A virtual environment is a tool to keep the dependencies required by different projects in separate places. The virtual environment tool in Python is called virtualenv.

Learn how to set up a virtual environment and install the correct libraries to achieve a consistent Python development environment.

  1. Install the virtualenv tool.
    pip install virtualenv

  2. Initialize your environment.
    virtualenv myproject
    This installs python, pip, and basic libraries within the project folder, myproject.

  3. Activate your environment.

    source myproject
    /
    venv
    /
    bin
    /
    activate
    

    Your prompt will change to show that you are now working in a virtual environment. You'll see something like:

    (
    venv
    )
     user@machine $
    
  4. Install libraries for the project.

    pip install 
    <
    library 
    file
    >
    
  5. Create a requirements.txt file to remember the dependencies.

    pip freeze 
    >
     myproject
    /
    requirements
    .
    txt
    

    The requirements.txt file contains the necessary dependencies for your project. It will look something like this:

    appdirs
    ==
    1.4
    .3
    
     asn1crypto
    ==
    0.22
    .0
    
     cffi
    ==
    1.10
    .0
    
     cryptography
    ==
    1.8
    .1
    
     enum34
    ==
    1.1
    .6
    
     idna
    ==
    2.5
    
     ipaddress
    ==
    1.0
    .18
    
     jsontree
    ==
    0.4
    .3
    
     ndg
    -
    httpsclient
    ==
    0.4
    .2
    
     packaging
    ==
    16.8
    
     pyasn1
    ==
    0.2
    .3
    
     pycparser
    ==
    2.17
    
     pyOpenSSL
    ==
    17.0
    .0
    
     pyparsing
    ==
    2.2
    .0
    
     requests
    ==
    2.14
    .2
    
     six
    ==
    1.10
    .0
    
     urllib3
    ==
    1.21
    .1
    
  6. Check the requirements.txt file along with the project code into your repository.
    If you, or another developer, add functionality that requires a new library, you'll add it to this requirements.txt file. That way, when other developers pull the update from the source code repository, they'll be aware of the new dependencies and able to install them easily.

Developer install

Any developer can use the virtual environment you created. The project setup is straightforward.

  1. To create the virtual environment, first install virtualenv.
    pip install virtualenv

  2. Create a folder and activate the virtual environment.

    virtualenv myproject source myproject
    /
    venv
    /
    bin
    /
    activate
    
  3. Check out the latest code from the repository.
    git clone
    Navigate to the directory that contains the project.

    (
    venv
    )
     user@machine $ cd myproject
    /
    myrepo
    
  4. Install the dependencies.

    pip 
    -
    r requirements
    .
    txt
    

Did this page help you?