Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How python uses gzip Compression

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report