MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap
Revision 4 as of 2015-03-12 21:33:12
  • Python
  • Requests

Requests

Requests is an Apache2 Licensed HTTP library, written in Python, for human beings.

http://docs.python-requests.org/en/latest/

Installation in Slackware

  • easy_install requests # python3
  • easy_install-2.7 requests

or

  • easy_install pip
  • pip install requests

Python3 test:

$ python3
Python 3.4.0 (default, May  9 2014, 21:04:05) 
[GCC 4.7.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.3.0'
>>> quit()

Python 2.7 test:

$ python2.7
Python 2.7.3 (default, Jul  3 2012, 21:16:07) 
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.3.0'
>>> quit()

Sample app in pyunit

   1 #filename: requestsTest.py
   2 #Run tests: python3 requestsTest.py
   3 import unittest
   4 import requests
   5 #import json
   6 
   7 class SimpleTestCase(unittest.TestCase):
   8     def setUp(self):
   9         self.geocodeurl='http://maps.googleapis.com/maps/api/geocode/json'
  10 
  11     def tearDown(self):
  12         pass
  13 
  14     def testCorroios(self):
  15         payload = {'address': 'Corroios', 'sensor': 'false'}
  16         r = requests.get(self.geocodeurl,params=payload)
  17         print( r.text  )
  18         self.assertEqual(True, len(r.text)>0  )
  19         objx = r.json() #json.loads(r.text)
  20         self.assertEqual('2855-117 Corroios, Portugal' , objx['results'][0]['formatted_address'] )
  21 
  22     def testAltoDoMoinho(self):
  23         payload = {'address': 'Alto do Moinho,Setubal', 'sensor': 'false'}
  24         r = requests.get(self.geocodeurl,params=payload)
  25         print( r.text  )
  26         self.assertEqual(True, len(r.text)>0  )
  27         objx = r.json() #json.loads(r.text)
  28         self.assertEqual('Alto do Moinho, 2855, Portugal' , objx['results'][0]['formatted_address'] )
  29 
  30 
  31 if __name__ == '__main__':
  32     unittest.main()
  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01