This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from patrick--/master
Added Python 2.7 simple HTTP post example
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |