• 自定义协议解决tcp协议的粘包问题


    1. 这里是通过tcp文件传输案列,运用struct模块解决粘包问题

      # server端
      importsocket
      importjson
      importstruct

      sk= socket.socket()
      sk.bind(('127.0.0.1', 9000))
      sk.listen()
      conn, addr= sk.accept()

      msg_len= conn.recv(4)   # b'"x00x00x00'
      dic_len= struct.unpack('i', msg_len)[0]   # int类型
      msg= conn.recv(dic_len).decode('utf-8')
      msg= json.loads(msg)

      withopen(msg['filename'], 'wb') asf:
         whilemsg['filesize'] >0:
             content= conn.recv(1024)
             msg['filesize'] -= len(content)
             f.write(content)
      conn.close()
      sk.close()

      # client端
      importsocket
      importos
      importjson
      importstruct


      sk= socket.socket()
      sk.connect(('127.0.0.1', 9000))

      abs_path= r'你想要传输到服务器端的文件的绝对路径'
      filename= os.path.basename(abs_path)
      filesize= os.path.getsize(abs_path)
      dic= {'filename': filename, 'filesize': filesize}
      str_dic= json.dumps(dic)
      b_dic= str_dic.encode('utf-8')
      mlen= struct.pack('i', len(b_dic))
      sk.send(mlen)
      sk.send(b_dic)

      withopen(abs_path, mode='rb') asf:
         whilefilesize>0:
             content= f.read(1024)
             filesize-= len(content)
             sk.send(content)
      sk.close()
  • 相关阅读:
    html5 语义
    HTML Web Workers
    创建删除元素appendChild,removeChild,createElement,insertBefore
    getPos封装
    getPos,offsetTop
    HTML 中有用的字符实体
    ellipsis
    HTML 统一资源定位器
    width,clientWidth,offsetWidth
    .offsetLeft,.offsetTop
  • 原文地址:https://www.cnblogs.com/he-qing-qing/p/11005995.html
Copyright © 2020-2023  润新知