hd1

hd1/unitsWebapp

hd1
Nov 5th, 2015
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. import json
  2. import logging
  3. import re
  4. import subprocess
  5.  
  6. import web
  7. urls = (
  8.     '/([^/]+)/([^/]+)/([0-9]+)', 'Unit'
  9. )
  10.  
  11. app = web.application(urls, globals())
  12.  
  13. class Unit:
  14.     def GET(self, unit_from, unit_to, amount):
  15.         arg1 = '{0} {1}'.format(amount, unit_from)
  16.         arg2 = unit_to
  17.         cmd = '/usr/bin/units'
  18.         args = [cmd, arg1, arg2]
  19.         logging.debug(args)
  20.         output = subprocess.check_output(args)
  21.         logging.debug(output)
  22.         lines = output.split('\n')
  23.         logging.debug(lines)
  24.         line = lines[0]
  25.         logging.debug(line)
  26.         value = line[line.index('*')+2:]
  27.         logging.debug('value is {0}'.format(value))
  28.         logging.debug('{0} {1}'.format(value, unit_to))
  29.         ret = {'Amout to convert': amount, 'Unit to convert': unit_from, 'Final Amount': value, 'Final Unit': unit_to}
  30.         return json.dumps(ret)
  31.  
  32.  
  33. if __name__ == "__main__":
  34.     logging.basicConfig(level=logging.FATAL)
  35.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment