CherryPy Tutorial 7 - Give it a REST? Fails?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

CherryPy Tutorial 7 - Give it a REST? Fails?

jibunnokage
CherryPy Tutorial 7 - Give it a REST?  Fails?  Any assistance appreciated.

# python3 --version
Python 3.5.3

# cat /etc/os-release
cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Reference...

Source, added configuration to use port 90, and allow external connection...
import random
import string
import cherrypy
@cherrypy.expose
class StringGeneratorWebService(object):
        @cherrypy.tools.accept(media='text/plain')
        def GET(self):
                return cherrypy.session['mystring']
        def POST(self, length=8):
                some_string = ''.join(random.sample(string.hexdigits, int(length)))
                cherrypy.session['mystring'] = some_string
                return some_string
        def PUT(self, another_string):
                cherrypy.session['mystring'] = another_string
        def DELETE(self):
                cherrypy.session.pop('mystring', None)
if __name__ == '__main__':
        conf = {
                '/': {
                        'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
                        'tools.sessions.on': True,
                        'tools.response_headers.on': True,
                        'tools.response_headers.headers': [('Content-Type', 'text/plain')],
                }
        }
        cherrypy.config.update({
                'server.socket_host': '0.0.0.0',
                'server.socket_port': 90,
        })
        cherrypy.quickstart(StringGeneratorWebService(), '/', conf)

Executed the file under python3, on Raspbian Debian 9 Stretch.  Get the following...

500 Internal Server Error

The server encountered an unexpected condition which prevented it from fulfilling the request.

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py", line 628, in respond
    self._do_respond(path_info)
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py", line 687, in _do_respond
    response.body = self.handler()
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/encoding.py", line 219, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 54, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "REST.py", line 11, in GET
    return cherrypy.session['mystring']
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/__init__.py", line 246, in __getitem__
    return child[key]
  File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/sessions.py", line 325, in __getitem__
    return self._data[key]
KeyError: 'mystring'

Powered by CherryPy 18.1.0 

--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: CherryPy Tutorial 7 - Give it a REST? Fails?

Tim Roberts
[hidden email] wrote:
>
> CherryPy Tutorial 7 - Give it a REST?  Fails?  Any assistance appreciated.

Did you do a PUT or a POST first to store a value?  There's no error
checking in the tutorial, so if you try to GET before you have stored a
value, the result would be what you see.  If you want to make it safer,
add something like:

     if 'mystring' in cherrypy.session:

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.

smime.p7s (5K) Download Attachment