How to add protocal info for webob.Response?

31 views
Skip to first unread message

peng...@gmail.com

unread,
Jul 22, 2017, 7:29:38 PM7/22/17
to pylons-discuss
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

unread,
Jul 22, 2017, 7:47:12 PM7/22/17
to pylons-...@googlegroups.com
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 pronounced:
------------------------
Steve Piercy, Soquel, CA

peng...@gmail.com

unread,
Jul 22, 2017, 8:05:56 PM7/22/17
to pylons-discuss
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)

Steve Piercy

unread,
Jul 22, 2017, 9:00:08 PM7/22/17
to pylons-...@googlegroups.com
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, peng...@gmail.com pronounced:
>>On 7/22/17 at 4:15 PM, peng...@gmail.com <javascript:> pronounced:

Bert JW Regeer

unread,
Jul 24, 2017, 12:24:53 AM7/24/17
to pylons-...@googlegroups.com
Hello Pengyu,

A Response() object by itself does not have any information about the HTTP protocol used to create the request. It is simply an object that implements the WSGI required handlers, which provides the information you see below:

1. Status Code
2. headers
3. body if applicable

The request environment will have information about the protocol if using a compliant HTTP -> WSGI gateway implementation.

The Response() objects __str__ implementation does not access the request environment, and thus can not provide the HTTP protocol used.

Is there a particular reason you’d like it to be included in the str output?

Bert

--
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-discus...@googlegroups.com.
To post to this group, send email to pylons-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/12b55319-4de8-493e-8484-c4d9e38ad9fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages