Tuesday, August 23, 2016

Curlify Your Python Requests Objects

I love things that are simple and useful. Egor's Curlify Python module is one of these gems I saw this morning.

I already have a Python script that demonstrate A10 Network's AXAPI using the Requests package:


With this package, I changed exactly one line of code wherever I wanted to give an curl example of the same operation.

For example, after importing the curlify file, instead of printing out r.content in line 23, I just print out curlify.to_curl(r.reqeust) and boom, I have a curl command I can pass to people! How cool is that?

7 import requests, json, curlify
 17 # Acquire athorization token
 18 auth_headers = {'content-type': 'application/json'}
 19 auth_payload = {"credentials": {"username": "admin", "password": "a10"}}
 20 auth_endpoint = '/axapi/v3/auth'
 21 url = base_url + auth_endpoint
 22 r = requests.post(url, data=json.dumps(auth_payload), headers=auth_headers, verify=Fa    lse)
 23 print(curlify.to_curl(r.request))

Of course, you get the most out of it if you use Requests already (who isnt?) and you can just write it in Requests. When somebody else wants a simpler operation, say using just curl, that you can just easily dish it out to them in less than 5 minutes.

This is my final output from the same Python file above:

The only different from the plain output is I added a '-k' to the curl operation because I have a self-signed certificate on the A10 Thunder TPS.

Nice tool, nice output, no sweat. My type of thing. :)

No comments:

Post a Comment