ge0rgeJ

PiFace - Garden_Irrigation REST API Call

Aug 31st, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1.     import httplib
  2.     import base64
  3.     import string
  4.     import urllib
  5.      
  6.     host = "api.supertweet.net"
  7.     url = "/1.1/statuses/update.json"
  8.     username = 'Your_Twitter_Account'
  9.     password = 'SuperTweet_Password'
  10.     message = 'Message Goes Here'
  11.      
  12.     # base64 encode the username and password
  13.     auth = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
  14.      
  15.     body = urllib.urlencode({'status': message})
  16.  
  17.     webservice = httplib.HTTP(host)
  18.     # write your headers
  19.     webservice.putrequest("POST", url)
  20.     webservice.putheader("Host", host)
  21.     webservice.putheader("User-Agent", "Python http auth")
  22.     webservice.putheader("Content-type", "application/x-www-form-urlencoded")
  23.     webservice.putheader("Content-length", "%d" % len(body))
  24.     # write the Authorization header like: 'Basic base64encode(username + ':' + password)
  25.     webservice.putheader("Authorization", "Basic %s" % auth)
  26.      
  27.     webservice.endheaders()
  28.     webservice.send(body)
  29.     # get the response
  30.     statuscode, statusmessage, header = webservice.getreply()
  31.     print "Response: ", statuscode, statusmessage
  32.     print "Headers: ", header
  33.     res = webservice.getfile().read()
  34.     print 'Content: ', res
Advertisement
Add Comment
Please, Sign In to add comment