2016-11-14 40 views
0

我想發送一個波斯語(波斯語)字符串與套接字從python到另一個python在樹莓。我在終端看到一些問號,當在txt文件中寫入時,我看到了ÇÝ - 個字符(對於一個字)。 服務器代碼:從python發送波斯字符串到python

# -*- 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

你應該發佈你的代碼,以便我們看到你做了什麼。 – SergiyKolesnikov

+0

您將您的波斯語符號編碼爲UTF16表示形式,然後通過套接字發送並再次將其解碼爲波斯語符號。例如,如果你想要UTF16十進制符號,用** int(repr(data.decode('utf-16'))[4:8],16)**解碼它,當你的「data」是波斯語簽署 –

+0

@ Dr.JohnJamesCobra,如果我想字符串不是int,我必須寫字符串(repr(data.decode('utf-16'))[4:8],16)???以及我如何編碼代碼? – narges

回答

0

我使用# -*- coding: utf-8 -*-而不是# -*- coding: cp1256 -*-在兩個代碼(客戶端和服務器)的頂部。所以沒有使用解碼和編碼功能,它就能正常工作。