Skip to content

Instantly share code, notes, and snippets.

@dvdme
Last active September 3, 2016 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvdme/8b6d6fd03ccec5f31be712a34b485806 to your computer and use it in GitHub Desktop.
Save dvdme/8b6d6fd03ccec5f31be712a34b485806 to your computer and use it in GitHub Desktop.

External modules, requests, json, list comprehension and (simple) webservers

  • In this part we will talk about how to install external modules, installing and using one of the most popular ones, requests, that is used to make http requests.

Install the requests module

pip install requests

Make a get request and check its response code, size, and data

import requests

r = requests.get('http://www.google.com')
r.status_code
print 'Text size %s' % (len(r.text))
r.text
  • After getting a json reply from a http get request, we will quickly create a json object from it and access its data.

But it would be more insteresting to get a response in json format

r = requests.get('http://api.fixer.io/latest')
r.status_code
print 'Text size %s' % (len(r.text))
r.text
rates = r.json()

We can check all the rates available

for r in rates['rates']:
    print r, rates['rates'][r]

rates['rates'].items()
  • We can create a class to store rate values and fill it with the json data.
class Rate(object):
    def __init__(self, name, value):
        self._name = name
        self._value = value

Now we can make a list of objects with our rates

rates_list = []
for k in rates['rates']:
    rates_list.append(Rate(k, rates['rates'][k]))

rates_list
len(rates_list)
  • With this list of objects with can use list comprehension to filter values from our list.

We can quickly get the rate for 'AUD'

[v._value for v in rates_list if v._name == 'AUD']

Or rates that are bigger than 10

[v._name for v in rates_list if v._value > 10]
  • Finally, what if our test url was down? We can emulate the reply writing a very simple webserver with Flask or Tornado that reads a file and returns it as a get http response.

What is our test url was down? We can emulate using Flask and a file with the data

Flask code

from flask import Flask

app = Flask(__name__)

@app.route('/latest')
def latest():
    with open('rates.txt') as f:
        text = f.read()
        return text

if __name__ == '__main__':
    app.run()
python water.py

Now we should have a server running at localhost

r = r.requests.get('http://localhost:5000/latest')
r.text

For reference, Tornado code

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        with open('rates.txt') as f:
			text = f.read()
			return text

def make_app():
    return tornado.web.Application([
        (r'/latest', MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(5000)
    tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment