Server 端:
...
import socket
s = socket.socket()
host = socket.gethostname()
port = 222;
s.bind((host,port))
s.listen(5)
while True:
c,addr = s.accept()
print('Got connection',addr)
c.send('thank you for connection'.encode('utf-8'))
c.close()
Client端:
import socket
s = socket.socket()
host = socket.gethostname()
port = 222
s.connect((host,port))
print(s.recv(1024))
s.close
中间出现过TypeError: 'str' does not support the buffer interface
stackoverflow中解释是Python3.X的str需要转换为utf-8才能识别,所以才c.send中添加了转换。
连接:
http://stackoverflow.com/questions/5471158/typeerror-str-does-not-support-the-buffer-interface