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('https://github.com/timeline.json', 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"
<skip>
>>> r.encoding
'utf-8'
>>>
>>> r = requests.post("http://httpbin.org/post")
>>>
>>> r = requests.put("http://httpbin.org/put")
>>> r = requests.delete("http://httpbin.org/delete")
>>> r = requests.head("http://httpbin.org/get")
>>> r = requests.options("http://httpbin.org/get")
>>>
** Request with Key:Value in Payload **

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.get("http://httpbin.org/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": "httpbin.org",\n    "Connection": "close"\n  },\n  "url": "http://httpbin.org/get?key2=value2&key1=value1",\n  "origin": "216.9.0.16"\n}'
>>> import json
>>> r.json()
{u'url': u'http://httpbin.org/get?key2=value2&key1=value1', 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'httpbin.org'}, u'args': {u'key2': u'value2', u'key1': u'value1'}, u'origin': u'216.9.0.16'}
>>> 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": "httpbin.org",\n    "Connection": "close"\n  },\n  "url": "http://httpbin.org/get?key2=value2&key1=value1",\n  "origin": "216.9.0.16"\n}'
>>>

*** Construct Image from binary return object **

>>> from PIL import Image
>>> from StringIO import StringIO
>>> i = Image.open(StringIO(r.content))

*** Raw content ***

>>> r = requests.get('https://github.com/timeline.json', stream=True, verify=False)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x2f255b0>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
>>>

Happy Coding! 



No comments:

Post a Comment