Giter Site home page Giter Site logo

bottle-websocket's Introduction

This project adds websocket capabilities to bottle, leveraging gevent-websocket and gevent.

Install

Use pip or easy_install:

pip install bottle-websocket

Usage

Usage is pretty straight-forward, just import the server and plugin:

from bottle.ext.websocket import GeventWebSocketServer
from bottle.ext.websocket import websocket

You can use the websocket plugin to turn routes websocket handlers, the websocket is passed to the route as the first argument:

@get('/websocket', apply=[websocket])
def echo(ws):
    while True:
        msg = ws.receive()
        if msg is not None:
            ws.send(msg)
        else: break

And then use the provided server:

run(host='127.0.0.1', port=8080, server=GeventWebSocketServer)

Contributors

bottle-websocket's People

Contributors

engineerjoe440 avatar nkanaev avatar quininer avatar zeekay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bottle-websocket's Issues

hey~ python3.5 is not work

hey firend i very like this package

but python3.5 error python2.7 is ok so the package is not work for python3.5 ?

python3.5 not work

duke@duke-uf /mnt/TT/p2p $ python3.5 ser.py 
Bottle v0.12.9 server starting up (using GeventWebSocketServer())...
Listening on http://127.0.0.1:8001/
Hit Ctrl-C to quit.

Traceback (most recent call last):
  File "/usr/lib64/python3.5/site-packages/gevent/greenlet.py", line 534, in run
    result = self._run(*self.args, **self.kwargs)
  File "/usr/lib64/python3.5/site-packages/gevent/baseserver.py", line 25, in _handle_and_close_when_done
    return handle(*args_tuple)
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 1253, in handle
    handler.handle()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 443, in handle
    result = self.handle_one_request()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 658, in handle_one_request
    self.handle_one_response()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 916, in handle_one_response
    self.log_request()
  File "/usr/lib64/python3.5/site-packages/geventwebsocket/handler.py", line 236, in log_request
    if '101' not in self.status:
TypeError: a bytes-like object is required, not 'str'
<Greenlet at 0x7fe9fed65af8: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with TypeError

Traceback (most recent call last):
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 884, in handle_one_response
    self.run_application()
  File "/usr/lib64/python3.5/site-packages/geventwebsocket/handler.py", line 70, in run_application
    self.result = self.upgrade_websocket()
  File "/usr/lib64/python3.5/site-packages/geventwebsocket/handler.py", line 129, in upgrade_websocket
    return self.upgrade_connection()
  File "/usr/lib64/python3.5/site-packages/geventwebsocket/handler.py", line 219, in upgrade_connection
    hashlib.sha1(key + self.GUID).digest())),
TypeError: Unicode-objects must be encoded before hashing
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
 'HTTP_ACCEPT_LANGUAGE': 'zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4',
 'HTTP_CACHE_CONTROL': 'no-cache',
 'HTTP_CONNECTION': 'Upgrade',
 'HTTP_HOST': '127.0.0.1:8001',
 'HTTP_ORIGIN': 'http://localhost:8001',
 'HTTP_PRAGMA': 'no-cache',
 'HTTP_SEC_WEBSOCKET_EXTENSIONS': 'permessage-deflate; client_max_window_bits',
 'HTTP_SEC_WEBSOCKET_KEY': 'GqFj9N4XhFEex2uEF7Xs/A==',
 'HTTP_SEC_WEBSOCKET_VERSION': '13',
 'HTTP_UPGRADE': 'websocket',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
                    '(KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36',
 'PATH_INFO': '/websocket',
 'QUERY_STRING': '',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '38446',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '',
 'SERVER_NAME': 'localhost',
 'SERVER_PORT': '8001',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.1 Python/3.5',
 'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x7fe9fdc51948>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0),
 'wsgi.websocket': <geventwebsocket.websocket.WebSocket object at 0x7fe9fd7b1250>,
 'wsgi.websocket_version': '13'} failed with TypeError

Traceback (most recent call last):
  File "/usr/lib64/python3.5/site-packages/gevent/greenlet.py", line 534, in run
    result = self._run(*self.args, **self.kwargs)
  File "/usr/lib64/python3.5/site-packages/gevent/baseserver.py", line 25, in _handle_and_close_when_done
    return handle(*args_tuple)
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 1253, in handle
    handler.handle()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 443, in handle
    result = self.handle_one_request()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 658, in handle_one_request
    self.handle_one_response()
  File "/usr/lib64/python3.5/site-packages/gevent/pywsgi.py", line 916, in handle_one_response
    self.log_request()
  File "/usr/lib64/python3.5/site-packages/geventwebsocket/handler.py", line 236, in log_request
    if '101' not in self.status:
TypeError: a bytes-like object is required, not 'str'
<Greenlet at 0x7fe9fdc55df0: _handle_and_close_when_done(<bound method WSGIServer.handle of <WSGIServer at , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with TypeError


Cannot switch to a different thread

I'm catching the above error reproducible if a client, which has connected to my GeventWebsockeServer based server, is no longer receiving data, while the server is sending. My websocket server pushes every 100 ms e.g. to a browser app. If I close the lid of PC wich runs the browser client, the a.m. error appears on each send after 30 seconds. Then, if the remote client is listening again, it recovers after another 30 secs.

Anything I can do better here?

using with ssl ?

Is it possible to use this to respond to wss:// ?
If yes, I would like to see a configuration example, as I have been unable to figure out something that works.

Pip install fails

It appears to be looking for the "Read Me" file, which is not included in the tarball.

# pip2 install bottle-websocket
Downloading/unpacking bottle-websocket
Downloading bottle-websocket-0.2.tar.gz
Running setup.py egg_info for package bottle-websocket
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build/bottle-websocket/setup.py", line 10, in <module>
long_description=open('README.md').read(),
IOError: [Errno 2] No such file or directory: 'README.md'
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 16, in <module>

File "/tmp/pip-build/bottle-websocket/setup.py", line 10, in <module>

long_description=open('README.md').read(),

IOError: [Errno 2] No such file or directory: 'README.md'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/bottle-websocket

Conflit gevent and bottle-websocket

mining/mining#89
Error:

<h1>Critical error while processing request: /stream/data/rwer</h1>Traceback (most recent call last):
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
self.run_application()
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/geventwebsocket/handler.py", line 76, in run_application
    self.run_websocket()
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/geventwebsocket/handler.py", line 52, in run_websocket
    self.application(self.environ, lambda s, h: [])
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/beaker/middleware.py", line 155, in __call__
    return self.wrap_app(environ, session_start_response)
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/bottle.py", line 979, in __call__
    return self.wsgi(environ, start_response)
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/bottle.py", line 974, in wsgi
    start_response('500 INTERNAL SERVER ERROR', headers, sys.exc_info())
  File "/home/avelino/.virtualenvs/mining/lib/python2.7/site-packages/beaker/middleware.py", line 154, in session_start_response
    return start_response(status, headers, exc_info)
TypeError: <lambda>() takes exactly 2 arguments (3 given)
{'GATEWAY_INTERFACE': 'CGI/1.1',
 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
 'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.5',
 'HTTP_CACHE_CONTROL': 'no-cache',
 'HTTP_CONNECTION': 'keep-alive, Upgrade',
 'HTTP_COOKIE': 'beaker.session.id=840c92edceec4c8580bd57722a46204f',
 'HTTP_DNT': '1',
 'HTTP_HOST': '127.0.0.1:8888',
 'HTTP_ORIGIN': 'http://127.0.0.1:8888',
 'HTTP_PRAGMA': 'no-cache',
 'HTTP_SEC_WEBSOCKET_KEY': 'beXNjFDC61V0WqdPYPkSLg==',
 'HTTP_SEC_WEBSOCKET_VERSION': '13',
 'HTTP_UPGRADE': 'websocket',
 'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
 'PATH_INFO': '/stream/data/rwer',
 'QUERY_STRING': 'page=1&',
 'REMOTE_ADDR': '127.0.0.1',
 'REMOTE_PORT': '34933',
 'REQUEST_METHOD': 'GET',
 'SCRIPT_NAME': '/',
 'SERVER_NAME': 'avelino-MacBookPro',
 'SERVER_PORT': '8888',
 'SERVER_PROTOCOL': 'HTTP/1.1',
 'SERVER_SOFTWARE': 'gevent/1.0 Python/2.7',
 'beaker.get_session': <bound method SessionMiddleware._get_session of <beaker.middleware.SessionMiddleware object at 0x3f386d0>>,
 'beaker.session': {'username': u'[email protected]', 'first_name': u'Thiago', 'last_name': u'Avelino', 'uid': u'AItOawkEk5CLcqdEN70kB8K9ysfCMy0mafnIjbQ', 'language': u'pt-br', 'profile_image': None, 'profile_url': None, 'profile_image_small': None, '_accessed_time': 1398189234.261992, 'email': u'[email protected]', '_creation_time': 1398189183.635779, 'screen_name': u'Thiago'},
 'bottle.app': <bottle.Bottle object at 0x3de5950>,
 'bottle.get': <bottle.FormsDict object at 0x405bdd0>,
 'bottle.raw_path': '/data/rwer',
 'bottle.request': <LocalRequest: GET http://127.0.0.1:8888/stream/data/rwer?page=1&>,
 'bottle.request.query': <bottle.FormsDict object at 0x405bdd0>,
 'bottle.request.urlparts': SplitResult(scheme='http', netloc='127.0.0.1:8888', path='/stream/data/rwer', query='page=1&', fragment=''),
 'bottle.route': <GET '/data/<slug>' <function data at 0x3dec9b0>>,
 'route.handle': <GET '/data/<slug>' <function data at 0x3dec9b0>>,
 'route.url_args': {'slug': 'rwer'},
 'wsgi.errors': <open file '<stderr>', mode 'w' at 0x7fb8ebafb1e0>,
 'wsgi.input': <gevent.pywsgi.Input object at 0x405b050>,
 'wsgi.multiprocess': False,
 'wsgi.multithread': False,
 'wsgi.run_once': False,
 'wsgi.url_scheme': 'http',
 'wsgi.version': (1, 0),
 'wsgi.websocket': None,
 'wsgi.websocket_version': '13'} failed with TypeError

How to configure with uwsgi?

The library works fine when used directly, and also when I use it through nginx. But when I try to run the app with uwsgi, I get None for the websocket (ws) in the handler. I just copied your echo handler for a quick test.

@app.get('/websocket', apply=[websocket], name='ws')
def echo(ws):
    while True:
        msg = ws.receive()
        print('got', msg)
        if msg is not None:
            ws.send(msg)
            print('sent', msg)
        else:
            break

I'm missing something in the configuration of uwsgi I guess. Do you have an example of running it with uwsgi?

IOError: [Errno 2] No such file or directory: 'README.md'

Error on install:

Downloading/unpacking bottle-websocket (from -r requirements.txt (line 7))
  Downloading bottle-websocket-0.2.6.tar.gz
  Running setup.py (path:/home/travis/virtualenv/python2.7/build/bottle-websocket/setup.py) egg_info for package bottle-websocket
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/home/travis/virtualenv/python2.7/build/bottle-websocket/setup.py", line 13, in <module>
        long_description=open('README.md'),
    IOError: [Errno 2] No such file or directory: 'README.md'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
  File "<string>", line 17, in <module>
  File "/home/travis/virtualenv/python2.7/build/bottle-websocket/setup.py", line 13, in <module>
    long_description=open('README.md'),
IOError: [Errno 2] No such file or directory: 'README.md'

https://travis-ci.org/avelino/mining/builds/22953486

WSS

how to use wss later?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.