Bottle + gevent unicode编码错误
Bottle + gevent unicode encoding error
我正在尝试 运行 本指南中的 websocket 示例代码:http://bottlepy.org/docs/dev/async.html
网站上提供的代码无效。我试着尽可能少地编写代码,但它没有任何改变。这是我的最终版本。
服务器端:
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()
客户端:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:8080/websocket");
</script>
</head>
</html>
服务器打印以下错误跟踪。出了什么问题?
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
只是猜测,但你试过了吗:
@app.route('/websocket')
def handle_websocket():
return b"Hello"
(附带说明,如果您关心性能,那么您应该 return 数组,而不是字符串。return [b"Hello"]
)
geventwebsocket 还没有完全支持 Python 3(截至 2016 年 5 月 2 日)。
https://bitbucket.org/noppo/gevent-websocket/issues/54/python-3-support#comment-None
我正在尝试 运行 本指南中的 websocket 示例代码:http://bottlepy.org/docs/dev/async.html
网站上提供的代码无效。我试着尽可能少地编写代码,但它没有任何改变。这是我的最终版本。
服务器端:
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()
客户端:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:8080/websocket");
</script>
</head>
</html>
服务器打印以下错误跟踪。出了什么问题?
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
只是猜测,但你试过了吗:
@app.route('/websocket')
def handle_websocket():
return b"Hello"
(附带说明,如果您关心性能,那么您应该 return 数组,而不是字符串。return [b"Hello"]
)
geventwebsocket 还没有完全支持 Python 3(截至 2016 年 5 月 2 日)。
https://bitbucket.org/noppo/gevent-websocket/issues/54/python-3-support#comment-None