1.客户端与服务端代码bug修复

客户端

import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

phone.connect(("127.0.0.1", 8081))

while True:
    msg = input(">>:").strip()
    phone.send(msg.encode("utf-8"))
    data = phone.recv(1024)
    print(data.decode("utf-8"))

phone.close()

 

服务端

 

import socket

phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

phone.bind(("127.0.0.1", 8081))

phone.listen(5)

conn, adr = phone.accept()

while True:
    data = conn.recv(1024)
    print("客户端的数据", data.decode("utf-8"))

    conn.send(data.upper())


conn.close()
phone.close()

总结: data.encode("utf-8") 转成bytes decode("utf-8") 进行中午字符解码

posted on 2018-08-20 09:33  我爱python,人生苦短  阅读(96)  评论(0编辑  收藏  举报