5.314 Python Samples

These samples were tested with version 2.9.1 of the requests package.

Simple request (GET)

#!/usr/bin/env python

from __future__ import print_function

import json
import sys

import requests

session = requests.Session()
session.auth = ('moab-admin', 'changeme!')
response = session.request(
    method='GET',
    url='http://localhost:8080/mws/rest/jobs',
    params={
        'query': json.dumps({'isActive': True}),
        'sort': json.dumps({'credentials.user': 1}),
        'fields': 'name,queueStatus,priorities.user,credentials.user',
        'max': 10,
        'api-version': 3,
    },
)
if response.ok:
    print(json.dumps(response.json(), sort_keys=True, indent=4))
else:
    try:
        print("Error: " + response.json()['messages'][0], file=sys.stderr)
    except ValueError:
        print("Error: status code is " + str(response.status_code), file=sys.stderr)

Complex request (POST)

#!/usr/bin/env python

from __future__ import print_function

import base64
import sys

import requests

session = requests.Session()
session.auth = ('moab-admin', 'changeme!')
script = base64.b64encode("""
#!/bin/sh
/bin/date
sleep 600
/bin/date
""")
response = session.request(
    method='POST',
    url='http://localhost:8080/mws/rest/jobs',
    params={'api-version': 3},
    json={
        'commandScript': script,
        'initialWorkingDirectory': '/tmp',
        'credentials': {
            'group': 'adaptive',
            'user': 'adaptive'
        },
        'requirements': [{'taskCount': 4}]
    })
if response.ok:
    print("Submitted job " + response.json()['name'])
else:
    try:
        print("Error: " + response.json()['messages'][0], file=sys.stderr)
    except ValueError:
        print("Error: status code is " + str(response.status_code), file=sys.stderr)

Related Topics 

© 2016 Adaptive Computing