2016-04-08 25 views
1

bu kılavuzdan WebSocket örnek kod çalıştırmak çalışıyorum: çalışmıyor sitede verilen http://bottlepy.org/docs/dev/async.htmlŞişe + gevent unicode kodlama hatası

kodu. Olabildiğince en azını kodlamayı denedim, ama hiçbir şey değiştirmedi. İşte son versiyonum.

sunucu tarafı:

from bottle import request, Bottle, abort 
app = Bottle() 

@app.route('/websocket') 
def handle_websocket(): 
    return "Hello" 

from gevent.pywsgi import WSGIServer 
from geventwebsocket import WebSocketError 
from geventwebsocket.handler import WebSocketHandler 
server = WSGIServer(("0.0.0.0", 8080), app, 
       handler_class=WebSocketHandler) 
server.serve_forever() 

istemci tarafı:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
    <script type="text/javascript"> 
    var ws = new WebSocket("ws://localhost:8080/websocket"); 
    </script> 
</head> 
</html> 

sunucu aşağıdaki hata iz basar. Neyin yanlış gidiyor?

Traceback (most recent call last): 
File "/usr/lib/python3.5/site-packages/gevent/pywsgi.py", line 884, in handle_one_response 
self.run_application() 
File "/usr/lib/python3.5/site-packages/geventwebsocket/handler.py", line 70, in run_application 
self.result = self.upgrade_websocket() 
File "/usr/lib/python3.5/site-packages/geventwebsocket/handler.py", line 129, in upgrade_websocket 
return self.upgrade_connection() 
File "/usr/lib/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': '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_HOST': 'localhost:8080', 
'HTTP_ORIGIN': 'null', 
'HTTP_PRAGMA': 'no-cache', 
'HTTP_SEC_WEBSOCKET_EXTENSIONS': 'permessage-deflate', 
'HTTP_SEC_WEBSOCKET_KEY': '8p+us6WWOyVrAJ+HOwqYog==', 
'HTTP_SEC_WEBSOCKET_VERSION': '13', 
'HTTP_UPGRADE': 'websocket', 
'HTTP_USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64; rv:40.0) Gecko/20100101 ' 
       'Firefox/40.0', 
'PATH_INFO': '/websocket', 
'QUERY_STRING': '', 
'REMOTE_ADDR': '127.0.0.1', 
'REMOTE_PORT': '59781', 
'REQUEST_METHOD': 'GET', 
'SCRIPT_NAME': '', 
'SERVER_NAME': 'localhost.localdomain', 
'SERVER_PORT': '8080', 
'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 0x7f0f9194e1c8>, 
'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 0x7f0f9194f250>, 
'wsgi.websocket_version': '13'} failed with TypeError 
Traceback (most recent call last): 
File "/usr/lib/python3.5/site-packages/gevent/greenlet.py", line 534, in run 
    result = self._run(*self.args, **self.kwargs) 
File "/usr/lib/python3.5/site-packages/gevent/baseserver.py", line 25, in _handle_and_close_when_done 
return handle(*args_tuple) 
    File "/usr/lib/python3.5/site-packages/gevent/pywsgi.py", line 1253, in handle 
handler.handle() 
    File "/usr/lib/python3.5/site-packages/gevent/pywsgi.py", line 443, in handle 
result = self.handle_one_request() 
    File "/usr/lib/python3.5/site-packages/gevent/pywsgi.py", line 658, in handle_one_request 
self.handle_one_response() 
    File "/usr/lib/python3.5/site-packages/gevent/pywsgi.py", line 916, in handle_one_response 
self.log_request() 
    File "/usr/lib/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 0x7f0f91bfa898: _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 

cevap

0
Sadece bir tahmin

ancak denediniz:

@app.route('/websocket') 
def handle_websocket(): 
    return b"Hello" 

(Performansta veriyorsan Yan not, o zaman bir dizi değil, bir dize dönmelidir return [b"Hello"].)

+0

sorun geventwebsocket beraber, operasyonel programlarda uygulamasıyla değildir. –