第一部分:服务端
# Author:qintao_xu
# date:2018/10/6
import socket
sk = socket.socket()
address = ('127.0.0.1', 9000)
sk.bind(address)
sk.listen(3)
print('waitting......')
while True:
conn, addr = sk.accept()
print('新来一个',addr)
while True:
data = conn.recv(1024)
if not data:
print('关闭了一个链接')
conn.close()
break
print(str(data, 'utf8'))
inp = input('>>>>')
conn.send(bytes(inp, 'utf8'))
第二部分客户端
# Author:qintao_xu
# date:2018/10/6
import socket
sk = socket.socket()
address = ('127.0.0.1', 9000)
sk.connect(address)
while True:
inp = input('>>>>')
if inp == 'exit':
break
sk.send(bytes(inp, 'utf8'))
data = sk.recv(1024)
print(str(data, 'utf8'))
else:
sk.close()
我是本地测试使用的 所以都是127.0.0.1