CherryPy and websockets

1,805 views
Skip to first unread message

Vincent Le Goff

unread,
Oct 30, 2012, 11:40:26 AM10/30/12
to cherrypy-users
Hi everyone,

I tried to find the answer on the Web but didn't come out with a lot
of informations: could CherryPy support websockets, be a webserver and
a websocket server listening on the same port?

Is that already possible on Cherrypy or is it a work in progress? Or,
on the other hand, could this functionality be implemented in a kind
of extension or plugin?

Happy hacking,

Vincent

Sylvain Hellegouarch

unread,
Oct 30, 2012, 11:49:23 AM10/30/12
to cherryp...@googlegroups.com
Hi Vincent,
CherryPy doesn't implement WebSocket indeed but I wrote a small package that works rather well and implements RFC 6455.


Note that only the official protocol is supported not older broken version, which means that some browser won't work with ws4py. See: http://caniuse.com/websockets

ws4py runs on top of CherryPy 3.2.x with Python 2x and 3.x.

--
- Sylvain
http://www.defuze.org
http://twitter.com/lawouach

Vincent Le Goff

unread,
Oct 30, 2012, 12:32:05 PM10/30/12
to cherryp...@googlegroups.com
Hi Sylvain,

Thanks, that's great, really great :D . I just have an error while
trying the echo_cherrypy example.

I have CherryPy 3.2 (Python 3.2). The example does start and
everything's working fine until I launch Firefox on localhost:9000.
Here's the error:

[30/Oct/2012:17:26:59] HTTP Traceback (most recent call last):
File "C:\python32\lib\site-packages\cherrypy-3.2.2-py3.2.egg\cherrypy\_cpreque
st.py", line 647, in respond
self.hooks.run('before_request_body')
File "C:\python32\lib\site-packages\cherrypy-3.2.2-py3.2.egg\cherrypy\_cpreque
st.py", line 112, in run
raise exc
File "C:\python32\lib\site-packages\cherrypy-3.2.2-py3.2.egg\cherrypy\_cpreque
st.py", line 102, in run
hook()
File "C:\python32\lib\site-packages\cherrypy-3.2.2-py3.2.egg\cherrypy\_cpreque
st.py", line 62, in __call__
return self.callback(**self.kwargs)
File "C:\python32\lib\site-packages\ws4py-0.2.3-py3.2.egg\ws4py\server\cherrypyserver.py",
line 149, in upgrade
ws_key = base64.b64decode(key)
File "C:\python32\lib\base64.py", line 83, in b64decode
raise TypeError("expected bytes, not %s" % s.__class__.__name__)
TypeError: expected bytes, not str
127.0.0.1 - - [30/Oct/2012:17:26:59] "GET /ws HTTP/1.1" 500 1686 "" "Mozilla/5.0

Maybe it's because the Python 3 sockets exchange bytes, no more str,
but I doubt of it, as Cherrypy handles it well. Do you know where
this problem come from?

Thanks again,

Vincent

2012/10/30, Sylvain Hellegouarch <s...@defuze.org>:
> --
> You received this message because you are subscribed to the Google Groups
> "cherrypy-users" group.
> To post to this group, send email to cherryp...@googlegroups.com.
> To unsubscribe from this group, send email to
> cherrypy-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cherrypy-users?hl=en.
>
>

Sylvain Hellegouarch

unread,
Oct 30, 2012, 12:36:45 PM10/30/12
to cherryp...@googlegroups.com
Hi Vincent,

I'll admit that I've mostly worked against Python 3.3 and may have overlooked a few things with 3.2. I will try to see that this evening. Note also that I'm usually running the latest CherryPy code rather than a stable version so that might be it as well. Damn :p

Ralph Heinkel

unread,
Oct 30, 2012, 1:32:14 PM10/30/12
to cherryp...@googlegroups.com
Hi Vincent,

we got websockets working really nicely with Sylvain's ws4py module.
However - and very unfortunately - we had to drop it again because the
websocket protocol was not supported by cherrypy behind mod_wsgi /
nginx at the time. I don't know what the situation is today, but I
doubt it has changed. I would love to see this working!

ciao ciao

Ralph

Sylvain Hellegouarch

unread,
Oct 30, 2012, 2:06:47 PM10/30/12
to cherryp...@googlegroups.com
Hi Ralph,

On Tue, Oct 30, 2012 at 6:32 PM, Ralph Heinkel <ralph....@web.de> wrote:
Hi Vincent,

we got websockets working really nicely with Sylvain's ws4py module.
However - and very unfortunately - we had to drop it again because the websocket protocol was not supported by cherrypy behind mod_wsgi / nginx at the time. I don't know what the situation is today, but I doubt it has changed. I would love to see this working!

It seems websockets is part of the 1.3 roadmap of nginx.  http://trac.nginx.org/nginx/roadmap
With a better support in nginx 1.2 for HTTP/1.1, maybe there is a chance reverse proxying now works well with websocket inside CherryPy.

Sylvain Hellegouarch

unread,
Oct 31, 2012, 4:52:26 AM10/31/12
to cherryp...@googlegroups.com
Hi Vincent,

I've reproduced the issue with Python 3.2 yesterday but didn't get a chance to fix it yet. Could you open a ticket on the ws4py github page please? 

Cheers,

Vincent Le Goff

unread,
Oct 31, 2012, 5:10:54 AM10/31/12
to cherryp...@googlegroups.com
Hi Sylvain and Ralph,

Sure, I'll do just that :) .

Thanks again for your work,

Vincent

2012/10/31, Sylvain Hellegouarch <s...@defuze.org>:

Sylvain Hellegouarch

unread,
Oct 31, 2012, 8:45:37 AM10/31/12
to cherryp...@googlegroups.com
Vincent,

On Wed, Oct 31, 2012 at 10:10 AM, Vincent Le Goff <vincent.l...@gmail.com> wrote:
Hi Sylvain and Ralph,

Sure, I'll do just that :) .

Thanks again for your work,

I've pushed a fix onto the repository. Could you try it by any chance?
 

Vincent Le Goff

unread,
Oct 31, 2012, 9:09:26 AM10/31/12
to cherryp...@googlegroups.com
Hi Sylvain,

The commit 54ab115d2db4a2d8f39475a14ff0062b386ea6e8 (fix #62) does not
solve the problem here, sorry :S .

Though, I tried to use sw4py on Python 3.3, installed it. The CherryPy
3.2.2 package doesn't work yet with Python 3.3.0 (at least not on
Windows) so I downloaded the last 'dev' version.

Your ech_cherrypy_server.py example works just fine under Python 3.3
and Cherrypy 3.2.3 (last dev). Maybe I should install the last
CherryPy version on python 3.2?

Thanks again for your work and your reactivity ;) ,

Vincent

2012/10/31, Sylvain Hellegouarch <s...@defuze.org>:

Sylvain Hellegouarch

unread,
Oct 31, 2012, 9:12:35 AM10/31/12
to cherryp...@googlegroups.com
On Wed, Oct 31, 2012 at 2:09 PM, Vincent Le Goff <vincent.l...@gmail.com> wrote:
Hi Sylvain,

The commit 54ab115d2db4a2d8f39475a14ff0062b386ea6e8 (fix #62) does not
solve the problem here, sorry :S .

Though, I tried to use sw4py on Python 3.3, installed it. The CherryPy
3.2.2 package doesn't work yet with Python 3.3.0 (at least not on
Windows) so I downloaded the last 'dev' version.

Your ech_cherrypy_server.py example works just fine under Python 3.3
and Cherrypy 3.2.3 (last dev).  Maybe I should install the last
CherryPy version on python 3.2?

Damn!

That's odd because I tried with Python 3.2 as provided by Ubuntu. Could you amend the ticket on github with any error you got with Python 3.2 after trying the last commit please? 

Vincent Le Goff

unread,
Oct 31, 2012, 10:20:20 AM10/31/12
to cherryp...@googlegroups.com
Actually it's the same error. But maybe it's due to my Cherrypy version.

Nevertheless, I'll try to find more informations

Vincent

2012/10/31, Sylvain Hellegouarch <s...@defuze.org>:

Sylvain Hellegouarch

unread,
Nov 3, 2012, 6:43:35 AM11/3/12
to cherryp...@googlegroups.com
Vincent,

On Wed, Oct 31, 2012 at 3:20 PM, Vincent Le Goff <vincent.l...@gmail.com> wrote:
Actually it's the same error. But maybe it's due to my Cherrypy version.

Nevertheless, I'll try to find more informations


I tried again this morning and it works well with Python 3.2.3, CherryPy 3.2.2 and ws4py 0.2.4 (current HEAD) against Firefox 16. 

Not sure what to tell you :)

Vincent Le Goff

unread,
Nov 4, 2012, 12:18:58 PM11/4/12
to cherryp...@googlegroups.com
Hi Sylvain,

I don't know what to tell me, either. But it's working now, with the
Cherrypy 3.2.3 development version.

Thanks again, wonderful work :) ,

Vincent

2012/11/3, Sylvain Hellegouarch <s...@defuze.org>:

Sylvain Hellegouarch

unread,
Nov 8, 2012, 5:16:39 AM11/8/12
to cherryp...@googlegroups.com
Hi Sander,



On Thu, Nov 8, 2012 at 11:11 AM, sanders <s.teu...@gmail.com> wrote:
Hi Sylvian,

I am facing the same issue.
So now I am trying to install version 0.24 (Cherrypy versions and Python version is the same). But a novice as I am, I downloaded the latest commit, but it has no setup.py with which I installed the 0.23 version....

Can you give me some guidance ?



Sure thing. I haven't yet released properly this new version so you'll have to grab the master branch. The easiest, if you don't have git, is to go to the download page: https://github.com/Lawouach/WebSocket-for-Python/downloads

This will get you ws4py with all you need. Simply run "python setup.py install" afterwards. 

Hope that helped.
Reply all
Reply to author
Forward
0 new messages