req.environ is a Python dictionary. Instead of iterating through all items in the dictionary, grab the key you need.

from webob import Request
req = Request.blank('/')
print(req.environ['SERVER_PROTOCOL'])
HTTP/1.0

--steve


On 7/22/17 at 5:05 PM, pengyu...@gmail.com pronounced:

Here is what I have tried. I don't see how to get the protocol with str(). Would you please show me the example code? Thanks.

$ cat ./main.py #!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

from webob import Request, Response

def my_app(environ, start_response):
req = Request(environ)
res = Response()
res.content_type = 'text/plain'
parts = []
for name, value in sorted(req.environ.items()):
parts.append('%s: %r' % (name, value))
res.body = '\n'.join(parts)
return res(environ, start_response)
req = Request.blank('/')
res = req.get_response(my_app)
print res
$  ./main.py 200 OK
Content-Type: text/plain; charset=UTF-8
Content-Length: 390

HTTP_HOST: 'localhost:80'
PATH_INFO: '/'
QUERY_STRING: ''
REQUEST_METHOD: 'GET'
SCRIPT_NAME: ''
SERVER_NAME: 'localhost'
SERVER_PORT: '80'
SERVER_PROTOCOL: 'HTTP/1.0'
wsgi.errors: <open file '<stderr>', mode 'w' at 0x1006ea1e0>
wsgi.input: <_io.BytesIO object at 0x100803ad0>
wsgi.multiprocess: False
wsgi.multithread: False
wsgi.run_once: False
wsgi.url_scheme: 'http'
wsgi.version: (1, 0)


On Saturday, July 22, 2017 at 6:47:12 PM UTC-5, Steve Piercy wrote:

Sure.  You can get it from the WSGI environment:

https://docs.pylonsproject.org/projects/webob/en/stable/reference.html#response-as-a-wsgi-
application

Or you can manually set attributes or headers in the response object. https://docs.pylonsproject.org/projects/webob/en/stable/api/response.html
--steve

On 7/22/17 at 4:15 PM, peng...@gmail.com <javascript:> pronounced:
The following example does not have the protocol info, e.g. 'HTTP/1.0'. Is there a way to add such info? $ cat main.py #!/usr/bin/env python # vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1
fileencoding=utf-8:

from webob import Response res = Response() print str(res)
$ ./main.py 200 OK Content-Type: text/html; charset=UTF-8 Content-Length: 0

------------------------ Steve Piercy, Soquel, CA



------------------------
Steve Piercy, Soquel, CA

--
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/r473Ps-10125i-1245D371867742BC80078F0B03710E0A%40Steves-iMac.local.
For more options, visit https://groups.google.com/d/optout.

Reply via email to