Jam.py documentation

on_ext_request

on_ext_request(task, request, params)

domain: server

language: python

class Task class

Description

Use on_ext_request to send a request to the server for processing.

The task parameter is a reference to the task tree The request is a string that must starts with ‘/ext’ There could be a list of parameters.

Example

The following application will send every 60 seconds a request to the server of Demo application

#!/usr/bin/env python

try:
    # For Python 3.0 and later
    from urllib.request import urlopen
except ImportError:
    # Fall back to Python 2's urllib2
    from urllib2 import urlopen
import json
import time

def send(url, request, params):
    a = urlopen(url + '/' + request, data=str.encode(json.dumps(params)))
    r = json.loads(a.read().decode())
    return r['result']['data']

if __name__ == '__main__':
    url = 'http://127.0.0.1:8080/ext'
    while True:
        result = send(url, 'get_sum', [1, 2, 3])
        print(result)
        time.sleep(60)

The server will process this request and return the sum of parameters. The on_ext_request must be declared in task server module:

def on_ext_request(task, request, params):
    #print request, params
    reqs = request.split('/')
    if reqs[2] == 'get_sum':
        return params[0] + params[1] + params[2]