Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
shrapnel
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
shrapnel
Commits
e6d0749f
Commit
e6d0749f
authored
Mar 27, 2012
by
Sam Rushing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
grid demo, shows POST data, compression, persistent connections, shared state
parent
9ff5bfb0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
79 additions
and
0 deletions
+79
-0
coro/http/demo/grid.py
coro/http/demo/grid.py
+79
-0
No files found.
coro/http/demo/grid.py
0 → 100644
View file @
e6d0749f
# -*- Mode: Python -*-
import
coro
import
coro.http
import
backdoor
# toy: move an X through a grid.
# tests: POST data, compression, persistent connections, shared state
import
sys
W
=
sys
.
stderr
.
write
class
grid_handler
:
def
__init__
(
self
,
w
,
h
):
self
.
w
=
w
self
.
h
=
h
self
.
grid
=
[[
'.'
for
x
in
range
(
w
)]
for
y
in
range
(
h
)]
self
.
pos
=
[
w
/
2
,
h
/
2
]
self
.
grid
[
self
.
pos
[
1
]][
self
.
pos
[
0
]]
=
'X'
def
match
(
self
,
request
):
return
request
.
path
.
startswith
(
'/grid'
)
def
handle_request
(
self
,
request
):
if
request
.
path
==
'/grid/source'
:
request
[
'content-type'
]
=
'text/plain'
request
.
set_deflate
()
request
.
push
(
open
(
'grid.py'
,
'rb'
).
read
())
request
.
done
()
return
request
[
'content-type'
]
=
'text/html'
request
.
set_deflate
()
if
request
.
file
:
data
=
request
.
file
.
read
()
pairs
=
[
x
.
split
(
'='
)
for
x
in
data
.
split
(
'&'
)
]
for
k
,
v
in
pairs
:
if
k
==
'dir'
:
x0
,
y0
=
self
.
pos
x1
,
y1
=
self
.
pos
if
v
==
'left'
:
x1
=
max
(
x0
-
1
,
0
)
elif
v
==
'right'
:
x1
=
min
(
x0
+
1
,
self
.
w
-
1
)
elif
v
==
'up'
:
y1
=
max
(
y0
-
1
,
0
)
elif
v
==
'down'
:
y1
=
min
(
y0
+
1
,
self
.
h
-
1
)
else
:
pass
self
.
grid
[
y0
][
x0
]
=
'*'
self
.
grid
[
y1
][
x1
]
=
'X'
self
.
pos
=
[
x1
,
y1
]
else
:
pass
l
=
[]
for
y
in
self
.
grid
:
l
.
append
(
''
.
join
(
y
))
request
.
push
(
'<pre>'
)
request
.
push
(
'
\
n
'
.
join
(
l
))
request
.
push
(
'
\
n
</pre>
\
n
'
)
request
.
push
(
'<form name="input" action="grid" method="post">'
'<input type="submit" name="dir" value="left" />'
'<input type="submit" name="dir" value="right" />'
'<input type="submit" name="dir" value="up" />'
'<input type="submit" name="dir" value="down" />'
'</form>'
'<a href="/grid/source">source for this handler</a>'
)
request
.
done
()
server
=
coro
.
http
.
server
()
server
.
push_handler
(
grid_handler
(
50
,
30
))
server
.
push_handler
(
coro
.
http
.
handlers
.
coro_status_handler
())
server
.
push_handler
(
coro
.
http
.
handlers
.
favicon_handler
())
coro
.
spawn
(
server
.
start
,
(
'0.0.0.0'
,
9001
))
coro
.
spawn
(
backdoor
.
serve
,
unix_path
=
'/tmp/httpd.bd'
)
coro
.
event_loop
(
30.0
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment