Saturday, April 5, 2014

Thumbs Up for Python Requests Package

On Reddit Python board, user Lukasa (correctly) pointed out that my A10 Script in posts A10 aXAPI Part 1 and A10 aXAPI Part 2 would be much shorter with Python Requests package instead of using standard library. As much as I'd like to stick with just the standard libraries, after all, 'batteries included' is one of the beautiful things with Python; after playing around with it a little bit, I totally wish I had used it earlier. "HTTP for Humans", sure, sign me up!

The main reason for me to try this out was to code against the new A10 ACOS3.0 API. I am more than happy to share the code, but ACOS3.0 is currently in EA, so I will share the examples once it is in GA and the Python SDK released.

In the mean time, the Requests Quick Start guide is one of the best I have seen. That was all I needed to get going. Below are repeating some of the quick start examples, it is worth pointing out that I did have the use the 'verify=False' option to bypass the SSL cert error. Honestly, a lot of them are pretty self-explanatory.. 'http for humans'. Ha!

>>> import requests
>>> r = requests.get('', verify=False)
>>> r.text
u'[{"created_at":"2014-04-01T18:31:39-07:00","payload":{"ref":"Create-Properties-Dir","ref_type":"branch","master_branch":"master","description":"The Omniwallet is a new type of web wallet that combines security, usability and multi-currency support.","pusher_type":"user"},"public":true,"type":"CreateEvent",”url"
>>> r.encoding
>>> r ="")
>>> r = requests.put("")
>>> r = requests.delete("")
>>> r = requests.head("")
>>> r = requests.options("")
** Request with Key:Value in Payload **

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("", params=payload)
>>> r.text
u'{\n  "args": {\n    "key1": "value1",\n    "key2": "value2"\n  },\n  "headers": {\n    "Accept": "*/*",\n    "X-Request-Id": "ec23d3c0-86fa-43cd-8d4f-6c5f71041b70",\n    "User-Agent": "python-requests/2.2.1 CPython/2.7.3 Darwin/13.1.0",\n    "Accept-Encoding": "gzip, deflate, compress",\n    "Host": "",\n    "Connection": "close"\n  },\n  "url": "",\n  "origin": ""\n}'
>>> import json
>>> r.json()
{u'url': u'', u'headers': {u'X-Request-Id': u'ec23d3c0-86fa-43cd-8d4f-6c5f71041b70', u'Accept-Encoding': u'gzip, deflate, compress', u'Connection': u'close', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.2.1 CPython/2.7.3 Darwin/13.1.0', u'Host': u''}, u'args': {u'key2': u'value2', u'key1': u'value1'}, u'origin': u''}
>>> r.content
'{\n  "args": {\n    "key1": "value1",\n    "key2": "value2"\n  },\n  "headers": {\n    "Accept": "*/*",\n    "X-Request-Id": "ec23d3c0-86fa-43cd-8d4f-6c5f71041b70",\n    "User-Agent": "python-requests/2.2.1 CPython/2.7.3 Darwin/13.1.0",\n    "Accept-Encoding": "gzip, deflate, compress",\n    "Host": "",\n    "Connection": "close"\n  },\n  "url": "",\n  "origin": ""\n}'

*** Construct Image from binary return object **

>>> from PIL import Image
>>> from StringIO import StringIO
>>> i =

*** Raw content ***

>>> r = requests.get('', stream=True, verify=False)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x2f255b0>

Happy Coding! 

No comments:

Post a Comment