edit-icon download-icon

SDK for Python

Last Updated: Apr 11, 2018

This topic provides a Python example.

Install the Python environment

The Python SDK is a pure Python library that supports all operating systems that run Python, mainly including Linux and Windows.

Install Python as follows:

  1. Download and install the latest Python 2 installer.

    Currently, the Python SDK supports Python 2.6 and 2.7. You can run python -V to check out the current version of Python.

  2. Download and install the Python package manager PIP.

    After PIP is installed, run pip -V to check if the installation is successful and query the PIP version.

Install the Python SDK dependency libraries

The Python SDK depends on a set of third-party Python libraries. Before using this SDK, install the following dependency libraries:

  • Google Protocol Buffer: The Python SDK depends on Protocol Buffer to write logs to the server. Run the following installation command:

    sudo pip install protobuf==2.5.0

    You must connect the PIP to the Python Package Index website to install Protocol Buffer. Ensure that you can access this website on your machine. If installation fails due to network problems, you can manually download and install Protocol Buffer from its GitHub site. For more information about the installation procedure, see the Readme document in the downloaded installation package.

  • Python-Requests: The Python SDK depends on Python-Requests to perform HTTP communication.Run the following installation command:

    sudo pip install requests

  • SimpleJson: The Python SDK depends on SimpleJson to process the results in JSON format returned by the API.Run the following installation command:

    sudo pip install simplejson

Install Python SDK

Download the Python SDK after you install the Python environment.

  1. Download the latest Python SDK package from GitHub.

  2. Extract the downloaded package to the specified directory.

  3. Run the following command in the preceding directory to install Python SDK:

    python setup.py install

Start a Python program

#!/usr/bin/env python
#encoding: utf-8

import datetime
from aliyun.log.logclient import LogClient
from aliyun.log.logitem import LogItem
from aliyun.log.putlogsrequest import PutLogsRequest

def main():
    #endpoint: Area of the storage where the data is written.
    #project and logstore: form the basic data storage unit.
    #accessKeyId and accessKeySecret: form the access key.
    #Note: Specify these parameters according to actual conditions.

    endpoint = "cn-hangzhou.log.aliyuncs.com"
    project = "proj-arms-7dd6ecb06d21e02aed9eeb56b79e9f"
    logstore = "logstore-56f96ec5546fb6555ef97dd057acb4e9"
    accessKeyId = "utmxiro7BYtTLLxL"
    accessKeySecret = "PyjsffdlggBoYcrgpr69w023b9UcBH"

    logGroupSize = 10
    examples = []
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Hangzhou&eventTeyp=1&Gender=1&Price=2140|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Houseware&Region=Shanghai&eventTeyp=3&Gender=0&Price=8305|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Foods&Region=Shenzhen&eventTeyp=3&Gender=1&Price=7121|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Shanghai&eventTeyp=3&Gender=1&Price=2917|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Foods&Region=Shanghai&eventTeyp=1&Gender=1&Price=4285|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Men's wear&Region=Hangzhou&eventTeyp=3&Gender=1&Price=7864|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Women's wear&Region=Hangzhou&eventTeyp=5&Gender=0&Price=2983|')
    examples.append('|c0a895e114526786450161001d1ed9|9|EADS|BIZ-MONITOR|0|Category=Foods&Region=Shenzhen&eventTeyp=5&Gender=1&Price=3201|')

    //Build a client.
    client = LogClient(endpoint, accessKeyId, accessKeySecret)

    for i in range(0, 10):
        logGroup = []
        for j in range(0, 10):
            logItem = LogItem()
            logItem.push_back("content", datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + examples[j % len(examples)])
            logGroup.append(logItem)
        req = PutLogsRequest(project, logstore, "", "", logGroup)
        client.put_logs(req)
    print "send data success"

if __name__ == '__main__':
    main()

Important parameter description

Parameter Description
endpoint Area where data is written (endpoint list)
accessKeyId Key ID for writing data
accessKeySecret Key passcode for writing data
project ID of the project to which data is written
logstore ID of the Logstore to which data is written

Note:

  • The accessKeyId and accessKeySecret issued by ARMS are not the Alibaba Cloud AK/SK, and must be obtained from ARMS. For more information, see SDK data source overview.
  • Project ID and Logstore ID jointly identify a unique data source.
Thank you! We've received your feedback.