Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into zstack

  • Loading branch information...
commit 4eae9c2d9c4bc12a79bc37010b2037c861a03840 2 parents 035280c + 3ea998f
Sam Rushing authored
View
2  coro/http/__init__.py
@@ -1,6 +1,6 @@
# -*- Mode: Python -*-
-from server import server, tlslite_server
+from server import server, tlslite_server, connection, http_request
import handlers
import coro
from coro import read_stream
View
20 coro/http/server.py
@@ -291,9 +291,9 @@ def error (self, code, reason=None):
s = self.DEFAULT_ERROR_MESSAGE % {
'code': code, 'message': message, 'reason': reason
}
- self['Content-Length'] = len(s)
- self['Content-Type'] = 'text/html'
- self.push (s)
+ self['content-length'] = str(len(s))
+ self['content-type'] = 'text/html'
+ self.push (s, flush=True)
self.done()
def log_line (self):
@@ -479,16 +479,16 @@ def start (self, addr, retries=5):
self.sock.listen (1024)
c = coro.spawn (self.run)
- c.set_name ('http_server (%s:%d)' % addr)
+ c.set_name ('%s (%s:%d)' % (self.__class__.__name__, addr[0], addr[1]))
def run (self):
self.thread_id = coro.current().thread_id()
while not self.shutdown_flag:
try:
conn, addr = self.accept()
- client = connection (self)
+ client = self.create_connection()
c = coro.spawn (client.run, conn, addr)
- c.set_name ('http connection on %r' % (addr,))
+ c.set_name ('%s connection on %r' % (self.__class__.__name__, addr,))
except coro.Shutdown:
break
except:
@@ -500,6 +500,9 @@ def run (self):
def accept (self):
return self.sock.accept()
+ def create_connection (self):
+ return connection (self)
+
def shutdown (self):
self.shutdown_flag = 1
try:
@@ -513,8 +516,9 @@ class tlslite_server (server):
"https server using the tlslite package"
- def __init__ (self, cert_path, key_path):
+ def __init__ (self, cert_path, key_path, **handshake_args):
server.__init__ (self)
+ self.handshake_args = handshake_args
self.cert_path = cert_path
self.key_path = key_path
self.read_chain()
@@ -524,7 +528,7 @@ def accept (self):
import tlslite
conn0, addr = server.accept (self)
conn = tlslite.TLSConnection (conn0)
- conn.handshakeServer (certChain=self.chain, privateKey=self.private)
+ conn.handshakeServer (certChain=self.chain, privateKey=self.private, **self.handshake_args)
return conn, addr
def read_chain (self):
View
24 docs/tutorial.rst
@@ -212,7 +212,7 @@ Servers and Clients
Echo Server
-----------
-Creating a server is easy (see `docs/tutorial/t1.py`_):
+Creating a server is easy (see :download:`docs/tutorial/t1.py <tutorial/t1.py>`):
.. sourcecode:: python
@@ -240,8 +240,6 @@ Creating a server is easy (see `docs/tutorial/t1.py`_):
coro.spawn (serve)
coro.event_loop()
-.. _docs/tutorial/t1.py: tutorial/t1.py
-
You can telnet into that server::
$ telnet localhost 9000
@@ -299,6 +297,10 @@ You can tell the system to exit::
>>> coro.set_exit()
>>> Connection closed by foreign host.
+[Note that this isn't just exiting the telnet/backdoor connection, but
+telling the entire system to shut down.]
+
+
.. note::
Try editing the echo server from above, so that it'll exit the
@@ -308,7 +310,7 @@ Echo Client
-----------
It's difficult to really beat on that server with your own fingers (and telnet).
-How about a client that'll exercise it a little (see `docs/tutorial/t2.py`_):
+How about a client that'll exercise it a little (see :download:`docs/tutorial/t2.py <tutorial/t2.py>`):
.. sourcecode:: python
@@ -337,8 +339,6 @@ How about a client that'll exercise it a little (see `docs/tutorial/t2.py`_):
coro.spawn (client)
coro.event_loop()
-.. _docs/tutorial/t2.py: tutorial/t2.py
-
Hit Ctrl-C to exit.
You should just see a hundred dots in the main window. You might get connection reset errors if the listen() parameter in the server wasn't high enough. If so, you could put some calls to sleep_relative() in there to stagger the creation of the clients.
@@ -348,9 +348,7 @@ Proxy Server
------------
This is a handy little server that lets you 'spy' on protocols. It's
-very handy when implementing protocols. See `docs/tutorial/proxy.py`_.
-
-.. _docs/tutorial/proxy.py: tutorial/proxy.py
+very handy when implementing protocols. See :download:`docs/tutorial/proxy.py <tutorial/proxy.py>`.
.. sourcecode:: python
@@ -473,9 +471,11 @@ Example of the full `profiler output`_. Note: each graph may be [re]sorted by c
The Killer Demo
===============
-See `docs/tutorial/worms.py`_ for a fun demo. Run the script from
+See :download:`docs/tutorial/worms.py <tutorial/worms.py>` for a fun demo. Run the script from
one terminal, and telnet into it from another terminal with a nice
-large window (your terminal needs to support ANSI escape codes).
+large window (your terminal needs to support ANSI escape codes)::
+
+ $ telnet localhost 9001
Each worm is its own thread, and each socket client has a separate
view into the shared 'arena'. This demo can easily handle hundreds of
@@ -516,8 +516,6 @@ separate worms (though things tend to get crowded)::
+=========================================================================+
keys: [q]uit [r]edraw [n]ew [c]ull [l]engthen [h]offa
-.. _docs/tutorial/worms.py: tutorial/worms.py
-
Here's the code controlling each worm's movement:
.. sourcecode:: python
View
26 docs/tutorial/worms.py
@@ -63,10 +63,14 @@ def populate (self, n=5):
x, y = self.random_pos()
self.worms.append (worm (self, x, y))
def cull (self, hoffa=False):
+ removed = []
for worm in self.worms:
if worm.stunned:
W ('culling worm %r\n' % (worm.chr,))
worm.kill (hoffa)
+ removed.append (worm)
+ for r in removed:
+ self.worms.remove (r)
class worm:
@@ -91,21 +95,18 @@ def __init__ (self, arena, x, y, length=10):
coro.spawn (self.go)
def go (self):
- try:
- while not self.exit:
- coro.sleep_relative (self.speed / 10000.0)
- if random.randrange (0,20) == 10:
+ while not self.exit:
+ coro.sleep_relative (self.speed / 10000.0)
+ if random.randrange (0,20) == 10:
+ if not self.turn():
+ return
+ else:
+ nx, ny = self.update()
+ while self.arena[(nx,ny)] != ' ':
if not self.turn():
return
- else:
nx, ny = self.update()
- while self.arena[(nx,ny)] != ' ':
- if not self.turn():
- return
- nx, ny = self.update()
- self.move ((nx, ny))
- finally:
- self.arena.worms.remove (self)
+ self.move ((nx, ny))
def update (self):
x, y = self.head
@@ -236,6 +237,7 @@ def serve():
s = coro.tcp_sock()
s.bind (('', 9001))
s.listen (5)
+ coro.write_stderr ('Try "telnet localhost 9001" to watch the worms!\n')
while 1:
c, a = s.accept()
t = terminal (c)
Please sign in to comment.
Something went wrong with that request. Please try again.