I have a javascript that sends unicode star character.
But python interprets it as letter â - both in console and in log file.
Other unicode characters are malformed as well.
var ws = new WebSocket("ws://127.0.0.1:8888/");
ws.onopen= function(){
console.log('\u2605');
ws.send('\u2605');
ws.close();
}
Code in python:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging, json
from websocket_server import WebsocketServer
def new_client(client, server):
print("Client id:"+ str(client["id"])+ " has connected")
def client_left(client, server):
print("Client id:"+ str(client["id"])+ " has diconnected")
logfile= open('C:/host/test-log.log', 'a', encoding='utf-8')
logging.basicConfig(stream=logfile,
level=logging.INFO,
format='%(message)s')
def msg_rcvd(client, server, msg):
print (msg);
logging.info(msg);
server = WebsocketServer(8888, host='127.0.0.1')
server.set_fn_new_client(new_client)
server.set_fn_message_received(msg_rcvd)
server.set_fn_client_left(client_left)
server.run_forever()
WebsocketServer lib used: https://github.com/Pithikos/python-websocket-server
Anyone knows what's the problem here?
EDIT:
Just found this page on git hub:
https://github.com/Pithikos/python-websocket-server/issues/19
It appears to be the lib issue and some of the users has posted the solution:
bytes([i^masks[idx % 4] for idx, i in enumerate(self.read_bytes(payload_length))]).decode()
However, I've absolutely no idea where to put this code.
Much appreciate help.
In the WebsocketServer library code this:
decoded = ""
for char in self.read_bytes(payload_length):
char ^= masks[len(decoded) % 4]
decoded += chr(char)
should be replaced with:
decoded=bytes([i^masks[idx % 4] for idx, i in enumerate(self.read_bytes(payload_length))]).decode()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.