key-value-store.py (798B)
1 """Key-Value store server. 2 3 The request takes "key=" and "value=" URL parameters. The key must be UUID 4 generated by token(). 5 6 - When only the "key=" is specified, serves a 200 response whose body contains 7 the stored value specified by the key. If the stored value doesn't exist, 8 serves a 200 response with an empty body. 9 - When both the "key=" and "value=" are specified, stores the pair and serves 10 a 200 response without body. 11 """ 12 13 14 def main(request, response): 15 key = request.GET.get(b"key") 16 value = request.GET.get(b"value", None) 17 18 # Store the value. 19 if value: 20 request.server.stash.put(key, value) 21 return (200, [], b"") 22 23 # Get the value. 24 data = request.server.stash.take(key) 25 if not data: 26 return (200, [], b"") 27 return (200, [], data)