一、一次通信

  1. # server端 v1
  2. import socket
  3. # 1、买手机
  4. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. # print(phone)
  6. # 2、绑定手机卡
  7. phone.bind(('127.0.0.1', 8081)) # 0-65535:0-1024给操作系统使用
  8. # 3、开机
  9. phone.listen(5) # 最大挂起连接数
  10. # 4、等电话连接
  11. print('starting...')
  12. conn, client_addr = phone.accept()
  13. # 5、收,发消息
  14. data = conn.recv(1024) # 1、单位:bytes 2、1024代表最大接收1024个bytes
  15. print('客户端的数据', data)
  16. conn.send(data.upper())
  17. # 6、挂电话
  18. conn.close()
  19. # 7、关机
  20. phone.close()
  1. # client端 v1
  2. import socket
  3. # 1、买手机
  4. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. # print(phone)
  6. # 2、拨号
  7. phone.connect(('127.0.0.1', 8081))
  8. # 3、发,收消息
  9. phone.send('hello'.encode('utf-8'))
  10. data = phone.recv(1024)
  11. print(data)
  12. # 4、关闭
  13. phone.close()

执行流程.gif

二、循环通信

v2.1

  1. # server端 v2.1
  2. import socket
  3. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. phone.bind(('127.0.0.1', 8083)) # 0-65535:0-1024给操作系统使用
  5. phone.listen(5)
  6. print('starting...')
  7. conn, client_addr = phone.accept()
  8. print(client_addr)
  9. while True: # 通信循环
  10. data = conn.recv(1024)
  11. print('客户端的数据', data)
  12. conn.send(data.upper())
  13. conn.close()
  14. phone.close()
  1. # client端 v2.1
  2. import socket
  3. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. phone.connect(('127.0.0.1', 8083))
  5. while True:
  6. msg = input('>>: ').strip()
  7. phone.send(msg.encode('utf-8'))
  8. data = phone.recv(1024)
  9. print(data)
  10. phone.close()

执行流程.gif

v2.2

解决bug

  1. # server端 v2.2
  2. import socket
  3. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # 系统回收端口需要时间,这里表示如果有端口则直接使用不创建端口
  5. phone.bind(('127.0.0.1', 8083)) # 0-65535:0-1024给操作系统使用
  6. phone.listen(5)
  7. print('starting...')
  8. conn, client_addr = phone.accept()
  9. print(client_addr)
  10. while True: # 通信循环
  11. try:
  12. data = conn.recv(1024)
  13. if not data: break # 适用于linux操作系统
  14. print('客户端的数据', data)
  15. conn.send(data.upper())
  16. except ConnectionResetError: # 适用于windows操作系统
  17. break
  18. conn.close()
  19. phone.close()
  1. # client端 v2.2
  2. import socket
  3. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. phone.connect(('127.0.0.1', 8083))
  5. while True:
  6. msg = input('>>: ').strip() # msg=''
  7. if not msg: continue
  8. phone.send(msg.encode('utf-8')) # phone.send(b'')
  9. # print('has send')
  10. data = phone.recv(1024)
  11. # print('has recv')
  12. print(data.decode('utf-8'))
  13. phone.close()

三、多客户端

可依次对多个客户端进行服务

  1. # server端 v3
  2. import socket
  3. phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  4. phone.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  5. phone.bind(('127.0.0.1', 8083)) # 0-65535:0-1024给操作系统使用
  6. phone.listen(5)
  7. print('starting...')
  8. while True: # 链接循环
  9. conn, client_addr = phone.accept()
  10. print(client_addr)
  11. while True: # 通信循环
  12. try:
  13. data = conn.recv(1024)
  14. if not data: break # 适用于linux操作系统
  15. print('客户端的数据', data)
  16. conn.send(data.upper())
  17. except ConnectionResetError: # 适用于windows操作系统
  18. break
  19. conn.close()
  20. phone.close()