如何显示通过websocket发送的UTF-8字符?

时间:2014-01-27 19:34:47

标签: python utf-8 websocket

我正在尝试构建一个简单的Web套接字服务器,该服务器加载一个包含一些推文的文件(作为CSV),然后通过websocket将该推文的字符串发送到Web浏览器。 Here is a gist with the sample that I'm using for testing.以下是Autobahn服务器组件(server.py):

import random
import time
from twisted.internet   import reactor
from autobahn.websocket import WebSocketServerFactory, \
                               WebSocketServerProtocol, \
                               listenWS


f = open("C:/mypath/parsed_tweets_sample.csv")

class TweetStreamProtocol(WebSocketServerProtocol):

    def sendTweet(self):
        tweet = f.readline().split(",")[2]
        self.sendMessage(tweet, binary=False)

    def onMessage(self, msg, binary):
        self.sendTweet() 

if __name__ == '__main__':

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = TweetStreamProtocol
   listenWS(factory)
   reactor.run()

这是网络组件(index.html):

<html>
   <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <script type="text/javascript"> 
            var ws = new WebSocket("ws://localhost:9000");

            ws.onmessage = function(e) {
               document.getElementById('msg').textContent = e.data; //unescape(encodeURIComponent(e.data));
               console.log("Got echo: " + e.data);
            }
      </script>
   </head>
   <body>
      <h3>Twitter Stream Visualization</h3>
      <div id="msg"></div>
      <button onclick='ws.send("tweetme");'>
         Get Tweet
      </button>
   </body>
</html>

当推文到达浏览器时,UTF-8字符未正确显示。如何修改这些简单脚本以在浏览器中显示正确的UTF-8字符?

2 个答案:

答案 0 :(得分:1)

这对我有用:

from autobahn.twisted.websocket import WebSocketServerProtocol, \
                                       WebSocketServerFactory


class TweetStreamProtocol(WebSocketServerProtocol):

   def sendTweets(self):
      for line in open('gistfile1.txt').readlines():
         ## decode UTF8 encoded file
         data = line.decode('utf8').split(',')

         ## now operate on data using Python string functions ..

         ## encode and send payload
         payload = data[2].encode('utf8')
         self.sendMessage(payload)

      self.sendMessage((u"\u03C0"*10).encode("utf8"))

   def onMessage(self, payload, isBinary):
      if payload == "tweetme":
         self.sendTweets()



if __name__ == '__main__':

   import sys

   from twisted.python import log
   from twisted.internet import reactor

   log.startLogging(sys.stdout)

   factory = WebSocketServerFactory("ws://localhost:9000", debug = False)
   factory.protocol = TweetStreamProtocol

   reactor.listenTCP(9000, factory)
   reactor.run()

注意:

  • 以上代码适用于Autobahn|Python 0.7及以上
  • 我不确定您是否对Gist进行了正确的UTF8编码文件
  • 但是,“最后一个”伪推文是10x“pi”,并且在浏览器中正确显示,所以 它原则上有效..

另请注意:由于原因太长,无法在此解释,如果sendMessage,高速公路的payload函数预期isBinary == False已经是UTF8编码。 “普通”Python字符串是Unicode,需要像上面那样编码为UTF8才能发送。

答案 1 :(得分:0)

而不是<meta http-equiv="content-type" content="text/html; charset=UTF-8"><尝试meta http-equiv="content-type" content="text/html; charset=UTF-8"> 如果您使用的是XHTML,请写下<meta charset = utf-8>