【发布时间】:2016-11-14 08:39:38
【问题描述】:
我想将带有套接字的波斯语(波斯语)字符串从 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)
我必须做什么?
【问题讨论】:
-
你应该发布你的代码,以便我们看到你做了什么。
-
您将波斯语符号编码为 UTF16 表示,然后通过套接字发送并再次将其解码为波斯语符号。例如,如果您想要符号的 UTF16 十进制,则在您的“数据”时使用 int(repr(data.decode('utf-16'))[4:8], 16)是波斯语符号
-
@Dr.JohnJamesCobra,如果我想要字符串不是 int,我必须写 string(repr(data.decode('utf-16'))[4:8], 16) ???以及我必须如何编码?
-
不是字符串而是“str”;您可以在 fileformat.info 使用 utf 表并在 docs.python.org/2/howto/unicode.html 找到编码示例
标签: python sockets encoding raspberry-pi3 persian