ExamplesExample 1 - Basic HTTP GET request: conn = HTTPConnection()
r = conn.request("get", "http://localhost/")
print "Response Headers:"
print r['headers']
print "Response Body:"
print r['body']Example 2 - Multiple PUT requests to resources sharing a common base URI: conn = HTTPConnection("http://10.2.3.4/thinglibrary/")
for thing in listofthings:
resource_path = 'things/%s' %(thing, )
print conn.build_uri(resource_path) # display full URL
r = conn.request("put", resource_path, body="test") # send PUT request to resource
print r['headers']['status'] # display response statusExample 3 - PUT an authenticated (basic auth) request containing JSON data jsonrec = json.dumps(data)
conn = HTTPConnection(username='username', password='password')
r = conn.request("put", "http://localhost/item/%s" %record_id, body=jsonrec.encode('utf-8'))
print "Response Headers:"
print r['headers']
print "Response Body:"
print r['body']
|