Skip to content

Instantly share code, notes, and snippets.

@dpryden
Created January 15, 2019 19:46
Show Gist options
  • Save dpryden/1dba7ff3f941545b818a61f6c46b7514 to your computer and use it in GitHub Desktop.
Save dpryden/1dba7ff3f941545b818a61f6c46b7514 to your computer and use it in GitHub Desktop.
Demo of a RESTful API server in Python using only the standard library.
#!/usr/bin/env python3
"""Demo of a RESTful API server in Python, using only the standard library.
The API here is extremely simple:
* a POST request stores a JSON object (from the POST body) at the given path
* a GET request returns a JSON object stored at the given path
Example usage:
# Save some JSON to /my/test
curl -X POST http://127.0.0.1:8080/my/test --data '{"test": 1}'
# Retrieve the saved JSON data
curl http://127.0.0.1:8080/my/test
"""
import http.server
import json
import traceback
class ApiRequestHandler(http.server.BaseHTTPRequestHandler):
"""Handle a single HTTP request to the server."""
def do_GET(self):
try:
data = self.server.storage.get(self.path)
if data is None:
self.send_response(404, 'Not Found')
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(b'{}')
return
self.send_response(200, 'OK')
self.send_header('Content-Type', 'application/json; charset=utf-8')
self.end_headers()
self.wfile.write(json.dumps(data).encode('utf-8'))
except:
traceback.print_exc()
def do_POST(self):
try:
length = int(self.headers['Content-Length'])
charset = self.headers.get_content_charset('ascii')
data = json.loads(self.rfile.read(length).decode(charset))
self.server.storage[self.path] = data
self.send_response(200, 'OK')
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(b'{}')
except:
traceback.print_exc()
class ApiServer(http.server.HTTPServer):
"""HTTP server for API requests."""
def __init__(self, host, port):
super().__init__((host, port), ApiRequestHandler)
# Create the dict that will hold the objects we store and retrieve.
self.storage = {}
def main():
"""Main program."""
server = ApiServer('127.0.0.1', 8080)
server.serve_forever()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment