使用从python到python的套接字发送波斯语字符串

时间:2016-11-14 08:39:38

标签: python sockets encoding raspberry-pi3 persian

我想将一个带有套接字的波斯语(波斯语)字符串从python发送到另一个覆盆子的python。我在终端看到一些问号,当写在txt文件中时,我看到ÇÝÔÇÑ字符(换言之)。 服务器代码:

# -*- coding: cp1256 -*-
import sys
import time
import os
import SocketServer
import thread
import os.path
import webbrowser
import RPi.GPIO as GPIO



    class MyTCPHandler(SocketServer.BaseRequestHandler):
        """
            The reques

t handler class for our server.

        It is instantiated once per connection to the server, and must
        override the handle() method to implement communication to the
        client.
        """

    def handle(self):
        try:
            # self.request is the TCP socket connected to the client
            self.data = self.request.recv(1024).strip()
            print "{} wrote:".format(self.client_address[0])


            text2=self.data

            print type(text2)

            text_file = open("Output.txt", "w")
            text_file.write(text2)
            text_file.close()
            completeURL="http://10.7.6.172/GanjSearch.aspx?q=/%s/" %text2
            print completeURL
            webbrowser.open(completeURL) 

        except Exception, e:
            print type(e)
            print e


if __name__ == "__main__":
    HOST, PORT = "192.168.1.55", 9998
    # Create the server, binding to localhost on port 9999
    try:
        server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
        # Activate the server; this will keep running until you
        # interrupt the program with Ctrl-C
        server.serve_forever()
    except Exception, e:
            print type(e)
            print e

客户代码:

a="persian-word"
socket.sendall(a)

我该怎么办?

1 个答案:

答案 0 :(得分:0)

我在两个代码(客户端和服务器)的顶部使用# -*- coding: utf-8 -*-而不是# -*- coding: cp1256 -*-。因此,如果不使用解码和编码功能,它可以正常工作。