Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example usage of contrib.http.server
- Loading branch information
Erik Allik
committed
Aug 27, 2013
1 parent
a72b5ab
commit d851ddb
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from gevent import sleep | ||
|
||
from spinoff.actor import Actor | ||
from spinoff.contrib.http.server import HttpServer | ||
|
||
|
||
class Main(Actor): | ||
def run(self): | ||
http_srv = self.spawn(HttpServer.using(address=('localhost', 8080), responders=[ | ||
(r'^/$', IndexResponder), | ||
(r'^/foo$', FooResponder), | ||
])) | ||
http_srv.join() | ||
|
||
|
||
class IndexResponder(Actor): | ||
def run(self, request): | ||
request.start_response('200 OK', [('Content-Type', 'text/html')]) | ||
request.write('index') | ||
request.close() | ||
|
||
|
||
class FooResponder(Actor): | ||
def run(self, request): | ||
request.start_response('200 OK', [('Content-Type', 'text/html')]) | ||
request.write('foo\n') | ||
self.spawn(SubResponder.using(request)) | ||
|
||
|
||
class SubResponder(Actor): | ||
def run(self, req): | ||
sleep(1.0) | ||
req.write('sub\n') | ||
req.close() |