Posted By

alejandrombernardis on 06/18/11


Tagged

python


Versions (?)

Py | SimpleRequest


 / Published in: Python
 

URL: http://www.alejandrob.com.ar/

  1. import tornado.escape # or simplejson, etc
  2. import urllib
  3.  
  4. class SimpleRequest(object):
  5.  
  6. def __init__(self, path, args={}, post_args=None):
  7. self._path = path
  8. self._args = args
  9. self._post_data = post_args
  10.  
  11. def read(self):
  12. post_data = None
  13.  
  14. if self._post_data is not None:
  15. post_data = urllib.urlencode(self._post_data)
  16.  
  17. path = "%s?%s" % (self._path, urllib.urlencode(self._args))
  18. request = urllib.urlopen(path, post_data)
  19.  
  20. try:
  21. response = tornado.escape.json_decode(request.read())
  22.  
  23. except:
  24. return False, -1
  25.  
  26. finally:
  27. request.close()
  28.  
  29. return True, response
  30.  
  31. @staticmethod
  32. def response(self, path, args={}, post_args=None):
  33. return SimpleRequest(path, args, post_args).read()

Report this snippet  

You need to login to post a comment.