CherryPy Tutorial 7 - Give it a REST? Fails?
[email protected] Thu, 28 Feb 2019 07:49:12 -0800 (PST)
| Newsgroups | gmane.comp.python.cherrypy |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_2600_879441781.1551368952774
Content-Type: multipart/alternative;
boundary="----=_Part_2601_339214840.1551368952774"
------=_Part_2601_339214840.1551368952774
Content-Type: text/plain; charset="UTF-8"
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...*
https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-7-give-us-a-rest
*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* <http://www.cherrypy.org/>
--
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 cherrypy-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To post to this group, send email to cherrypy-users-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
Visit this group at https://groups.google.com/group/cherrypy-users.
For more options, visit https://groups.google.com/d/optout.
------=_Part_2601_339214840.1551368952774
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>CherryPy Tutorial 7 - Give it a REST?=C2=A0 Fails?=C2=
=A0 Any assistance appreciated.</div><div><br></div><div><strong># python3 =
--version<br></strong>Python 3.5.3</div><div><br></div><div><strong># cat /=
etc/os-release</strong></div><div>cat /etc/os-release<br>PRETTY_NAME=3D&quo=
t;Raspbian GNU/Linux 9 (stretch)"<br>NAME=3D"Raspbian GNU/Linux&q=
uot;<br>VERSION_ID=3D"9"<br>VERSION=3D"9 (stretch)"<br>=
ID=3Draspbian<br>ID_LIKE=3Ddebian<br>HOME_URL=3D"<a href=3D"http://www=
.raspbian.org/">http://www.raspbian.org/</a>"<br>SUPPORT_URL=3D"<=
a href=3D"http://www.raspbian.org/RaspbianForums">http://www.raspbian.org/R=
aspbianForums</a>"<br>BUG_REPORT_URL=3D"<a href=3D"http://www.ras=
pbian.org/RaspbianBugs">http://www.raspbian.org/RaspbianBugs</a>"<br><=
/div><div><br></div><div><strong>Reference...</strong></div><div><a href=3D=
"https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-7-give-us-a-re=
st">https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-7-give-us-a=
-rest</a></div><div><br></div><div><strong>Source, added configuration to u=
se port 90, and allow external connection...</strong></div><div>import rand=
om<br>import string</div><div>import cherrypy</div><div>@cherrypy.expose<br=
>class StringGeneratorWebService(object):</div><div>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 @cherrypy.tools.accept(media=3D'text/plain')<=
br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 def GET(self):<br>=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 return cherrypy.session['mystring']</div><div>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 def POST(self, length=3D8):<br>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 some_string =3D ''.join(random.sample(string.hexdigits, int(len=
gth)))<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 cherrypy.session['mystring'] =3D some_s=
tring<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 return some_string</div><div>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 def PUT(self, another_string):<br>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ch=
errypy.session['mystring'] =3D another_string</div><div>=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 def DELETE(self):<br>=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ch=
errypy.session.pop('mystring', None)</div><div>if __name__ =3D=3D &=
#39;__main__':<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 conf =3D {=
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 '/': {<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'request.dispatch': cherrypy.dispatc=
h.MethodDispatcher(),<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 'tools.sessions.on': True,<br>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'tools.response_headers.on&#=
39;: True,<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 'tools.response_headers.headers': [('Content-Type', =
9;text/plain')],<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0 }</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 cherryp=
y.config.update({<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'server.socket_host': '0.0=
.0.0',<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'server.socket_port': 90,<br>=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 })</div><div>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 cherrypy.quickstart(StringGeneratorWebService(), '/&=
#39;, conf)</div><div><br></div><div><div><strong>Executed the file under p=
ython3, on Raspbian Debian 9 Stretch.=C2=A0 Get the following...</strong></=
div><div><br></div></div><div><h2>500 Internal Server Error</h2><p>The serv=
er encountered an unexpected condition which prevented it from fulfilling t=
he request.</p><pre id=3D"traceback">Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py&=
quot;, line 628, in respond
self._do_respond(path_info)
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py&=
quot;, line 687, in _do_respond
response.body =3D self.handler()
File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/encoding.p=
y", line 219, in __call__
self.body =3D 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&qu=
ot;, line 246, in __getitem__
return child[key]
File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/sessions.p=
y", line 325, in __getitem__
return self._data[key]
KeyError: 'mystring'</pre><pre><br></pre><pre>Powered by <a href=3D=
"http://www.cherrypy.org/"><u><font color=3D"#0066cc">CherryPy 18.1.0</font=
></u></a> </pre><pre><br></pre><pre></pre></div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;cherrypy-users" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:cherrypy-users+unsubscribe-/[email protected]">cher=
rypy-users+unsubscribe-/[email protected]</a>.<br />
To post to this group, send email to <a href=3D"mailto:cherrypy-users@googl=
egroups.com">cherrypy-users-/[email protected]</a>.<br />
Visit this group at <a href=3D"https://groups.google.com/group/cherrypy-use=
rs">https://groups.google.com/group/cherrypy-users</a>.<br />
For more options, visit <a href=3D"https://groups.google.com/d/optout">http=
s://groups.google.com/d/optout</a>.<br />
------=_Part_2601_339214840.1551368952774--
------=_Part_2600_879441781.1551368952774--