Python 瓶子+;gevent unicode编码错误

Python 瓶子+;gevent unicode编码错误,python,unicode,websocket,bottle,gevent,Python,Unicode,Websocket,Bottle,Gevent,我正在尝试运行本指南中的websocket示例代码: 网站上提供的代码不起作用。我试着尽可能少地编写代码,但没有任何改变。这是我的最终版本 服务器端: from bottle import request, Bottle, abort app = Bottle() @app.route('/websocket') def handle_websocket(): return "Hello" from gevent.pywsgi import WSGIServer from geven

我正在尝试运行本指南中的websocket示例代码:

网站上提供的代码不起作用。我试着尽可能少地编写代码,但没有任何改变。这是我的最终版本

服务器端:

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>

var ws=newwebsocket(“ws://localhost:8080/WebSocket”);
服务器打印以下错误跟踪。出什么事了

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
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.5/site packages/gevent/pywsgi.py”,第884行,在handle\u one\u响应中
self.run_应用程序()
文件“/usr/lib/python3.5/site packages/geventwebsocket/handler.py”,第70行,在run_应用程序中
self.result=self.upgrade\u websocket()
文件“/usr/lib/python3.5/site packages/geventwebsocket/handler.py”,第129行,在upgrade\u websocket中
返回self.upgrade\u连接()
文件“/usr/lib/python3.5/site packages/geventwebsocket/handler.py”,第219行,在升级连接中
hashlib.sha1(key+self.GUID).digest()),
TypeError:在散列之前必须对Unicode对象进行编码
{'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_缓存控制”:“无缓存”,
“HTTP_连接”:“保持活动状态,升级”,
“HTTP_HOST”:“localhost:8080”,
“HTTP_源代码”:“null”,
“HTTP_PRAGMA”:“无缓存”,
'HTTP_SEC_WEBSOCKET_EXTENSIONS':'permessage deflate',
“HTTP_SEC_WEBSOCKET_KEY”:“8p+us6WWOyVrAJ+HOwqYog==”,
“HTTP_SEC_WEBSOCKET_VERSION”:“13”,
“HTTP_升级”:“websocket”,
“HTTP_USER_AGENT”:“Mozilla/5.0(X11;Linux x86_64;rv:40.0)Gecko/20100101”
“Firefox/40.0”,
“路径信息”:“/websocket”,
“查询字符串”:“”,
“远程地址”:“127.0.0.1”,
“远程端口”:“59781”,
“请求方法”:“获取”,
“脚本名称”:“,
“服务器名称”:“localhost.localdomain”,
“服务器端口”:“8080”,
“服务器协议”:“HTTP/1.1”,
“服务器软件”:“gevent/1.1 Python/3.5”,
“wsgi.errors”:,
“wsgi.input”:,
“wsgi.multiprocess”:False,
'wsgi.multi-thread':False,
'wsgi.run_once':False,
'wsgi.url_scheme':'http',
“wsgi.version”:(1,0),
'wsgi.websocket':,
'wsgi.websocket_version':'13'}因TypeError失败
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.5/site packages/gevent/greenlet.py”,第534行,正在运行
结果=self.\u运行(*self.args,**self.kwargs)
文件“/usr/lib/python3.5/site packages/gevent/baseserver.py”,第25行,在“句柄”和“完成时关闭”
返回句柄(*args\u tuple)
文件“/usr/lib/python3.5/site packages/gevent/pywsgi.py”,第1253行,在handle中
handler.handle()
文件“/usr/lib/python3.5/site packages/gevent/pywsgi.py”,第443行,在handle中
结果=self.handle\u one\u请求()
文件“/usr/lib/python3.5/site packages/gevent/pywsgi.py”,第658行,在handle\u one\u请求中
self.handle\u one\u response()
文件“/usr/lib/python3.5/site packages/gevent/pywsgi.py”,第916行,在handle\u one\u响应中
self.log_请求()
文件“/usr/lib/python3.5/site packages/geventwebsocket/handler.py”,第236行,在日志请求中
如果“101”未处于自我状态:
TypeError:需要类似字节的对象,而不是“str”

只是一个猜测,但您是否尝试过:

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

(另一方面,如果您关心性能,那么应该返回一个数组,而不是字符串。
返回[b“Hello”]

geventwebsocket目前还没有对Python 3的完全支持(截至2016年5月2日)


问题不在于OPs应用程序,而在于geventwebsocket。