-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
117 lines (95 loc) · 3.29 KB
/
server.py
File metadata and controls
117 lines (95 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# coding: utf-8
# Copyright 2013 Abram Hindle
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You can start this by executing it in python:
# python server.py
#
# remember to:
# pip install flask
import flask
from flask import Flask, request, redirect
import json
app = Flask(__name__)
app.debug = True
# An example world
# {
# 'a':{'x':1, 'y':2},
# 'b':{'x':2, 'y':3}
# }
class World:
def __init__(self):
self.clear()
def update(self, entity, key, value):
entry = self.space.get(entity,dict())
entry[key] = value
self.space[entity] = entry
def set(self, entity, data):
self.space[entity] = data
def clear(self):
self.space = dict()
def get(self, entity):
return self.space.get(entity,dict())
def world(self):
return self.space
# you can test your webservice from the commandline
# curl -v -H "Content-Type: application/json" -X PUT http://127.0.0.1:5000/entity/X -d '{"x":1,"y":1}'
myWorld = World()
# I give this to you, this is how you get the raw body/data portion of a post in flask
# this should come with flask but whatever, it's not my project.
def flask_post_json():
'''Ah the joys of frameworks! They do so much work for you
that they get in the way of sane operation!'''
if (request.json != None):
return request.json
elif (request.data != None and request.data.decode("utf8") != u''):
return json.loads(request.data.decode("utf8"))
else:
return json.loads(request.form.keys()[0])
@app.route("/")
def hello():
'''Return something coherent here.. perhaps redirect to /static/index.html '''
#return None
#Source: https://stackoverflow.com/a/14343957
#Author: https://stackoverflow.com/users/16117401/hackdolphin
#Link: https://stackoverflow.com/questions/14343812/redirecting-to-url-in-flask
#Code Used
return redirect("/static/index.html", code=302)
#End Code Used
@app.route("/entity/<entity>", methods=['POST','PUT'])
def update(entity):
'''update the entities via this interface'''
data = flask_post_json()
myWorld.set(entity, data)
return json.dumps(myWorld.get(entity))
#return None
@app.route("/world", methods=['POST','GET'])
def world():
'''you should probably return the world here'''
return json.dumps(myWorld.world())
#return None
@app.route("/entity/<entity>")
def get_entity(entity):
'''This is the GET version of the entity interface, return a representation of the entity'''
return json.dumps(myWorld.get(entity))
#return None
@app.route("/clear", methods=['POST','GET'])
def clear():
'''Clear the world out!'''
myWorld.clear()
return json.dumps(myWorld.world())
#return None
if __name__ == "__main__":
app.run()