1、Server端
''' server端 先运行Server端 ''' import socket sk = socket.socket() sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) # 允许重用,避免服务重启的时候,address 已用,和允许的最大的连接数有关系,有的用完了就回收了,有的用完了就不回收 sk.bind(("127.0.0.1",9000)) sk.listen() # 监听,等待别人打电话 conn,addr = sk.accept() # 接收到别人电话,connection 连接 address地址 rec= conn.recv(1024) conn.send(b'hi') # 和别人说话,必须传一个bytes 类型 print(rec) conn.close() # 挂电话
2、client端
''' socket 通信 client端 ''' import socket sk = socket.socket() sk.connect(('127.0.0.1',9000)) # server端 sk.send(b'hello') rec = sk.recv(1024) print(rec) sk.close()
3、socket
、