Skip to content

Commit 672d52b

Browse files
authored
python 2.7 crashes handling error ConnectionResetError
Hi! I was running the server in python 2.7 but it crashed when it tried to handle the *ConnectionResetError*, because I think it is not defined in python 2.7. I'm proposing this fix, although I dont know if there's a better way to do it.
1 parent 433a826 commit 672d52b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

websocket_server/websocket_server.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from base64 import b64encode
77
from hashlib import sha1
88
import logging
9+
from socket import error as SocketError
10+
import errno
911

1012
if sys.version_info[0] < 3:
1113
from SocketServer import ThreadingMixIn, TCPServer, StreamRequestHandler
@@ -189,10 +191,13 @@ def read_bytes(self, num):
189191
def read_next_message(self):
190192
try:
191193
b1, b2 = self.read_bytes(2)
192-
except ConnectionResetError:
193-
logger.info("Client closed connection.")
194-
self.keep_alive = 0
195-
return
194+
except SocketError as e:
195+
if e.errno == errno.ECONNRESET:
196+
logger.info("Client closed connection.")
197+
print("Error: {}".format(e))
198+
self.keep_alive = 0
199+
return
200+
b1, b2 = 0, 0
196201
except ValueError as e:
197202
b1, b2 = 0, 0
198203

0 commit comments

Comments
 (0)