Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Merge pull request #57 from patrick--/master
Browse files Browse the repository at this point in the history
Added Python 2.7 simple HTTP post example
  • Loading branch information
toddtreece committed Jul 15, 2014
2 parents 8cc69ae + 61c70cf commit 791afdd
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/python/http_post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env python

""" Simple http POST example using Python 2.7 and urllib and urllib2."""

import urllib
import urllib2

public_hash = 'KJJGv81n1pUyM4Kbg9by'
private_hash = 'vzzjDZR6RpfjdEzb1Ybj'
base_url = 'https://data.sparkfun.com'
post_url = base_url + '/input/' + public_hash


headers = {
'Content-type': 'application/x-www-form-urlencoded',
'Phant-Private-Key': private_hash
}


def main():

data = {}
data['who'] = raw_input("Who are you? ")
data['where'] = raw_input("Where are you? ")
data['favorite_animal'] = raw_input("What's your favorite animal? ")
data['code'] = 'python'

data = urllib.urlencode(data)
post_request = urllib2.Request(post_url,data,headers)

try:
post_response = urllib2.urlopen(post_request)
print post_response.read()

except URLError as e:
print "Error: ", e.reason

if __name__ == "__main__":
main()

0 comments on commit 791afdd

Please sign in to comment.