Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses gzip Compression

Shulou Source: shulou.com Published: 2022-06-01 05:48:33 10月24日 Update

This article mainly shows you "python how to use gzip compression", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to use gzip compression" this article.

Gzip compression

Have you ever encountered some web pages, no matter how the transcoding is a mess. Haha, that means you don't know that many web services have the ability to send compressed data, which can reduce the large amount of data transmitted on network lines by more than 60%. This is especially true for XML web services because the compression ratio of XML data can be very high.

But the general server will not send compressed data for you unless you tell the server that you can handle the compressed data.

So you need to modify the code like this:

Import urllib2, httplibrequest = urllib2.Request ('http://xxxx.com')request.add_header('Accept-encoding',' gzip') opener = urllib2.build_opener () f = opener.open (request)

This is the key: create a Request object and add an Accept-encoding header to tell the server that you can accept gzip compressed data.

And then decompress the data:

Import StringIOimport gzipcompresseddata = f.read () compressedstream = StringIO.StringIO (compresseddata) gzipper = gzip.GzipFile (fileobj=compressedstream) print gzipper.read () these are all the contents of the article "how python uses gzip Compression". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Data service content server article learning help random code code information key object that is easy to understand more organization knowledge ribbon network network cable Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi OPPO Reno MariaDB macOS NVidia