VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • Python3标准库:zlib GNUzlib压缩(2)

lorem
  • print('Decompressed matches lorem:',decompressed_matches)
  •  
  • unused_matches = decompressor.unused_data == lorem
  • print('Unused data matches lorem:',unused_matches)
  • 解压缩所有数据后,unused_data属性会包含未用的所有数据。

    1.4 校验和

    除了压缩和解压缩函数,zlib还包括两个用于计算数据的校验和的函数,分别是adler32()和crc32()。这两个函数计算出的校验和都不能认为是密码安全的,它们只用于数据完整性验证。

    
    	
    1. import zlib
    2.  
    3. data = open('lorem.txt','rb').read()
    4.  
    5. cksum = zlib.adler32(data)
    6. print('Adler32: {:12d}'.format(cksum))
    7. print(' : {:12d}'.format(zlib.adler32(data,cksum)))
    8.  
    9. cksum = zlib.crc32(data)
    10. print('CRC-32: {:12d}'.format(cksum))
    11. print(' : {:12d}'.format(zlib.crc32(data,cksum)))

    这两个函数取相同的参数,包括一个包含数据的字节串和一个可选值,这个值可作为校验和的起点。这些函数会返回一个32位有符号整数值,这个值可以作为一个新的起点参数再传回给后续的调用,以生成一个动态变化的校验和。

    1.5 压缩网络数据

    下一个代码清单中的服务器使用流压缩器来响应文件名请求,它将文件的一个压缩版本写至与客户通信的套接字中。

    
    	
    1. import zlib
    2. import logging
    3. import socketserver
    4. import binascii
    5.  
    6. BLOCK_SIZE = 64
    7.  
    8.  
    9. class ZlibRequestHandler(socketserver.BaseRequestHandler):
    10.  
    11. logger = logging.getLogger('Server')
    12.  
    13. def handle(self):
    14. compressor = zlib.compressobj(1)
    15.  
    16. # Find out what file the client wants
    17. filename = self.request.recv(1024).decode('utf-8')
    18. self.logger.debug('client asked for: %r', filename)
    19.  
    20. # Send chunks of the file as they are compressed
    21. with open(filename, 'rb') as input:
    22. while True:
    23. block = input.read(BLOCK_SIZE)
    24. if not block:
    25. break
    26. self.logger.debug('RAW %r', block)
    27. compressed = compressor.compress(block)
    28. if compressed:
    29. self.logger.debug(
    30. 'SENDING %r',
    31. binascii.hexlify(compressed))
    32. self.request.send(compressed)
    33. else:
    34. self.logger.debug('BUFFERING')
    35.  
    36. # Send any data being buffered by the compressor
    37. remaining = compressor.flush()
    38. while remaining:
    39. to_send = remaining[:BLOCK_SIZE]
    40. remaining = remaining[BLOCK_SIZE:]
    41. self.logger.debug('FLUSHING %r',
    42. binascii.hexlify(to_send))
    43. self.request.send(to_send)
    44. return
    45.  
    46.  
    47. if __name__ == '__main__':
    48. import socket
    49. import threading
    50. from io import BytesIO
    51.  
    52. logging.basicConfig(
    53. level=logging.DEBUG,
    54. format='%(name)s: %(message)s',
    55. )
    56. logger = logging.getLogger('Client')
    57.  
    58. # Set up a server, running in a separate thread
    59. address = ('localhost', 0) # let the kernel assign a port
    60. server = socketserver.TCPServer(address, ZlibRequestHandler)
    61. ip, port = server.server_address # what port was assigned?
    62.  
    63. t = threading.Thread(target=server.serve_forever)
    64. t.setDaemon(True)
    65. t.start()
    
    相关教程