Network Programming章节错误

错误:The socket Module小节的示例代码Listing 14-1 A Minimal Server,和Listing 14-2 A Minimal Client有误(python3中)

Listing 14-1 A Minimal Server(原著代码)

import socket

s = socket.socket()

host = socket.gethostname()
port = 1234
s.bind((host, port))

s.listen(5)

while True:
    cli_skt, addr = s.accept()
    print('Got connection from', addr)
    cli_skt.send('Thank you for connecting')
    cli_skt.close()

其中cli_skt.send('Thank you for connecting')应当改成cli_skt.send('Thank you for connecting'.encode())

Listing 14-2 A Minimal Client(原著代码)

import socket

s = socket.socket()

host = socket.gethostname()

port = 1234

s.connect((host, port))
print(s.recv(1024))

其中print(s.recv(1024))应当改成print(s.recv(1024).decode())

本书第三版是基于python3出版发行的,不应该出现这样的错误!可见老外发行的计算机书籍也不都是完美的,我已近发现好几处错误了。但是总的来讲英文原版的计算机书籍读起来更流畅


你可能感兴趣的:(Network Programming章节错误)