看到这个提示之后,确定了这个问题主要是是字符编码的问题了,那如何解决这个字符编码问题,目前想到的办法就是通过encode和decode来做字符编码转换了。
# cat tcp_server.py
#! /bin/env python3
# -*- coding:utf-8 -*-
'''
Created on 2016年12月19日
@author: CC
'''
import socket
import datetime
HOST='0.0.0.0'
PORT=3434
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)
while True:
conn,addr=s.accept()
print("Client %s connected!" % str(addr))
dt=datetime.datetime.now()
message="Current time is " + str(dt)
conn.send(message.encode('utf-8'))
print("Sent: ",message)
conn.close()
# cat tcp_client.py
#! /bin/env python3
# -*- coding:utf-8 -*-
'''
Created on 2016年12月19日
@author: CC
'''
import socket
HOST='127.0.0.1'
PORT=3434
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST,PORT))
print("Connect %s:%d OK" % (HOST,PORT))
data=s.recv(1024)
print("Received: ",data.decode('utf-8'))
s.close()
注:大家可以试试在客户端不加decode的效果啦,如果还有其他方法,还请大家多多解惑呀