Well, I think I've succeeded in writing the world's most trivial middleware.  :-)    Once I got into this, I realized that I didn't want to re-issue a request just to dump the environ.  Instead, I save off the environ on every request and access it as needed.
<br><br>One issue that comes up with this approach is EnvPrinter will have to be the last middleware in the stack or the environ won&#39;t be complete; the test author will just have to know this requirement.&nbsp; Of course, it&#39;s possible someone might want to dump the environ from the middle of the stack, too, so maybe this is more of a feature than a bug.
<br><br>A working example is below.&nbsp; I&#39;m including everything in one script as a demo, with a namespace hack to show where I&#39;d put things in a finished product. <br><br>If you don&#39;t see any design flaws in this, I can work on patches for a finished product if you&#39;d like.
<br><br>thanks,<br>-Ken<br><br><br><br>import twill<br>import twill.extensions<br><br>try:<br>&nbsp;&nbsp;&nbsp; import wsgi_intercept<br>except ImportError:<br>&nbsp;&nbsp;&nbsp; from twill import wsgi_intercept<br><br># Hack up a psuedo-module for twill_wsgi namespace,
<br>#&nbsp;&nbsp;&nbsp; to show where I&#39;d be putting things eventually.<br>class X(object): pass<br>twill.extensions.twill_wsgi = X()<br><br>def print_env(env_var):<br>&nbsp;&nbsp;&nbsp; return wsgi_intercept.last_request_environ[env_var]<br>twill.extensions.twill_wsgi.print_environ
 = print_env<br><br><br>wsgi_intercept.last_request_environ = None<br>class EnvPrinter:<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Super simple middleware to save off the request&#39;s wsgi environ.&quot;&quot;&quot;<br><br>&nbsp;&nbsp;&nbsp; def __init__(self, app):
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.wrapped_app = app<br><br>&nbsp;&nbsp;&nbsp; def __call__(self, environ, start_response):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; global last_request_environ<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wsgi_intercept.last_request_environ = environ<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return self.wrapped_app
(environ, start_response)<br>wsgi_intercept.EnvPrinter = EnvPrinter<br><br><br>###########<br># Test it.<br>###########<br>def simple_app(environ, start_response):<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;Simplest possible application object&quot;&quot;&quot;
<br>&nbsp;&nbsp;&nbsp; status = &#39;200 OK&#39;<br>&nbsp;&nbsp;&nbsp; response_headers = [(&#39;Content-type&#39;,&#39;text/plain&#39;)]<br>&nbsp;&nbsp;&nbsp; environ[&#39;simple_app.myval&#39;] = True<br>&nbsp;&nbsp;&nbsp; start_response(status, response_headers)<br><br>&nbsp;&nbsp;&nbsp; return [&#39;WSGI intercept successful!\n&#39;]
<br><br>def test_print_environ():<br>&nbsp;&nbsp;&nbsp; &quot;&quot;&quot; Test access to the wsgi environment with wsgi_intercept. &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp; app_fn = wsgi_intercept.EnvPrinter(simple_app)<br>&nbsp;&nbsp;&nbsp; twill.add_wsgi_intercept
(&#39;localhost&#39;, 80, lambda: app_fn)<br><br>&nbsp;&nbsp;&nbsp; print &#39;go&#39;<br>&nbsp;&nbsp;&nbsp; twill.commands.go(&#39;<a href="http://localhost:80/&#39;">http://localhost:80/&#39;</a>)<br><br>&nbsp;&nbsp;&nbsp; print &#39;print_environ&#39;<br>&nbsp;&nbsp;&nbsp; val = 
twill.extensions.twill_wsgi.print_environ(&#39;simple_app.myval&#39;)<br>&nbsp;&nbsp;&nbsp; assert val == True, val<br><br>&nbsp;&nbsp;&nbsp; print &#39;remove&#39;<br>&nbsp;&nbsp;&nbsp; twill.remove_wsgi_intercept(&#39;localhost&#39;, 80)<br><br><br># TODO: Implement twill script, python api access.&nbsp; script example:
<br>#&gt;&gt; extend_with twill_wsgi<br>#&gt;&gt; print_environ foo_name env_variable_to_print<br><br><br>