今天敲了书《Python基础教程第二版》 14章网络编程代码14-1,如下,稍微修改了些:
客户端:
import socket
s = socket.socket()
host = '127.0.0.1'
port = 1234
s.connect((host, port))
print(s.recv(1024))
服务端:
import socket
s = socket.socket()
host = '127.0.0.1'
port = 1234
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from ', addr)
c.send('Thank you for connecting')
c.close()
网上查资料说是send函数发数据的问题,将send发出的字符串以utf-8的格式encode后就好了:
Server端程序修改:
c.send('Thank you for connecting'.encode('utf-8'))
print(s.recv(1024).decode('utf-8'))