-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME_simplerest.html
More file actions
223 lines (151 loc) · 5.33 KB
/
README_simplerest.html
File metadata and controls
223 lines (151 loc) · 5.33 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<pre>
goal: make python rest server as basis for implementing web services
I want a multi-threaded python web server to serve rest services. I
want multi-threaded for the purpose of possible long-polling in which
one thread will wait for content while others can continue to serve.
================================
I want an easy rest interface.
NOTE: I used emacs restclient.el but it simply using CURL is more
straightforward.
https://github.com/pashky/restclient.el
git clone https://github.com/pashky/restclient.el.git
(load-file "/home/mpsb/Desktop/rest-python/restclient.el/restclient.el")
###GET http://httpbin.org/ip
M-x restclient-http-send-current-stay-in-window
M-x restclient-http-send-current-raw
Works!
================================
I did some similar README_web-command.html. copy here to
"restPython.py" and update GET/POST from
/media/datastore/storestudio/workspace2015/webfilemanager/webfilemanager.py
python3 -m simplerest.server
----
GET http://localhost:8080/service?foo=bar
###
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.6
Date: Fri, 16 Sep 2016 17:53:57 GMT
default response
['path:/service?foo=bar', 'form:["foo=[\'bar\'];"]', "environ:{'QUERY_STRING': 'foo=bar', 'REQUEST_METHOD': 'GET'}"]
----
POST http://localhost:8080/xxx
foo2=bar2&blah=zap
###
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.6
Date: Fri, 16 Sep 2016 17:54:11 GMT
default response
['path:/xxx', "form:['blah=zap;', 'foo2=bar2;']", 'environ:{}']
################################
Now look at getting key/value pairs.
----
GET http://localhost:8080/getkey?key=foo
###
Trying to get key that does not exist
"error mykey not in keystate"
----
GET http://localhost:8080/setkey?key=foo;value=bar
###
Setting the key
"done setkey foo bar"
----
GET http://localhost:8080/getkey?key=foo
###
Getting a new key
"getkey foo = bar"
----
GET http://localhost:8080/getkey?key=foo
###
Getting an "old" key blocks. server writes "getkey sleeping..." until
key is new by setkey... and then responds (by setkey below)
"getkey foo = zapzap"
----
GET http://localhost:8080/getkey?key=foo;immediate=true
###
Immediate get the key even though the other get is still blocked.
"getkey foo = bar"
----
GET http://localhost:8080/setkey?key=foo;value=zapzap
###
setting the key to a new value. any blocked get then responds.
"done setkey foo zapzap"
################################
Now do updating of specific functions:
import restPython
class myHandler(restPython.RestHandler):
def action_myfunction( self ):
self.message = "myfunction here!"
server = restPython.ThreadedHTTPServer(('localhost', 8080), myHandler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()
GET http://localhost:8080/myfunction
###
HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.6
Date: Wed, 28 Sep 2016 23:09:59 GMT
myfunction here!
################################
Add general file serving. This will allow me to send the
"Access-Control-Allow-Origin: *" header so that xhr can access
anything rather than the origininating page (CORS).
GET http://192.168.11.16:8080/service?foo=bar
####
GET http://192.168.11.16:8080/README_rest-python.html
####
GET http://192.168.11.16:8080/
####
################################################################
Package it up so I can call it
Following README_python-install.html.
Move everything into simplerest/ and make blank __init__.py so it is
treated as a python package (directory containing stuff) and rename
restpython.py to server.py (server is the module in the simplerest
package). Note I make a package only because I had the extra
SimpleFileResponse.py file. Otherwise I could have only had a single
file module.
Now you can do: "python -m simplerest.server". I think this make more
sense than if the module and the package had the same name: "python -m
simplerest.simplerest"
Check:
python setup.py sdist
python3 setup.py install --user
Yep seems to work.
================================
Put on github:
on github create project "simplerest"
> https://github.com/michaelbrownid/simplerest.git
Create .gitignore
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/michaelbrownid/simplerest.git
git push -u origin master
# Done!
----------------
Try from home directory:
python -m simplerest.server
/usr/bin/python: No module named simplerest
# install into ~/.local with no sudo
pip install --user git+https://github.com/michaelbrownid/simplerest.git --upgrade
Collecting git+https://github.com/michaelbrownid/simplerest.git
Cloning https://github.com/michaelbrownid/simplerest.git to /tmp/pip-10WZ0d-build
Installing collected packages: simplerest
Running setup.py install for simplerest ... [?25ldone
[?25hSuccessfully installed simplerest
python -m simplerest.server
args {'p': 8080, 'd': './', 'h': '192.168.11.16'}
Starting server, use <Ctrl-C> to stop
YAY!
================================
Have server broadcast and then pick up the address.
python3 -m simplerest.server --broadcast 1
# broadcasts 1/second on port 6789
other process:
python3 -m simplerest.broadcast
# this will listen for broadcast, print the server location, and then turn off broadcasting
receiver init 6789
receiver.listen
got data: b'8080,isanyoneout' from addr: ('192.168.11.16', 42529)
GOT BROADCAST
server: http://192.168.11.16:8080/
turning off broadcast: curl 'http://192.168.11.16:8080/setkey?key=broadcast&value=0''