使用从 python 到 python 的套接字发送波斯语字符串
Sending persian string with socket from python to python
我想用套接字从 python 发送一个波斯语 (farsi) 字符串到树莓派中的另一个 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)
我必须做什么?
我在两个代码(客户端和服务器)的顶部使用 # -*- coding: utf-8 -*-
而不是 # -*- coding: cp1256 -*-
。所以在不使用解码和编码功能的情况下,它可以正常工作。
我想用套接字从 python 发送一个波斯语 (farsi) 字符串到树莓派中的另一个 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)
我必须做什么?
我在两个代码(客户端和服务器)的顶部使用 # -*- coding: utf-8 -*-
而不是 # -*- coding: cp1256 -*-
。所以在不使用解码和编码功能的情况下,它可以正常工作。