客户端
1 #客户端 2 import socket 3 4 client = socket.socket()#相当于声明socket类型,同时生成socket连接对象 5 client.connect((‘localhost‘,6969)) 6 7 client.send(b"Hello World!") 8 data = client.recv(1024) 9 print("recv:",data) 10 11 client.close()
服务端
1 #服务器端 2 import socket 3 4 server = socket.socket() 5 server.bind(("localhost",6969))#绑定要监听的端口 6 server.listen()#监听 7 8 print("我要开始等电话了") 9 10 conn,addr = server.accept()#等电话打进来 11 print(conn,addr) 12 13 print("电话来了") 14 15 data = conn.recv(1024) 16 print("recv:",data) 17 conn.send(data.upper()) 18 19 server.close()