close-code-and-reason_wsh.py 739 B

12345678910111213141516171819202122232425262728
  1. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import struct
  5. from mod_pywebsocket import stream
  6. def web_socket_do_extra_handshake(_request):
  7. pass
  8. def web_socket_transfer_data(request):
  9. line = request.ws_stream.receive_message()
  10. if line is None:
  11. return
  12. if line == '-':
  13. data = b''
  14. elif line == '--':
  15. data = b'X'
  16. else:
  17. code, reason = line.split(' ', 1)
  18. data = struct.pack('!H', int(code)) + reason.encode('utf-8')
  19. request.connection.write(stream.create_close_frame(data))
  20. request.server_terminated = True
  21. # Wait for Close frame from client.
  22. request.ws_stream.receive_message()