pope (owner)

Revisions

gist: 73994 Download_button fork
public
Public Clone URL: git://gist.github.com/73994.git
Embed All Files: show embed
Python #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import gzip
import zlib
from urlparse import urlparse
from httplib import HTTPConnection
 
def workding_download_gzip_file(url, filename):
  """Download a gzip file and decompress it
 
Save the gzip file, then read it in and re-save it
"""
 
  o = urlparse(url)
  conn = HTTPConnection(o.netloc)
  conn.request("GET", o.path)
  resp = conn.getresponse()
 
  data = resp.read()
 
  gz_filename = filename + ".gz"
 
  with open(gz_filename, "wb") as gf:
    gf.write(data)
 
  gf = gzip.open(gz_filename, "rb")
  data = gf.read()
  gf.close()
 
  os.remove(gz_filename)
 
  with open(filename, "w") as f:
    f.write(data)
 
 
def nonworkding_download_gzip_file(url, filename):
  """Download a gzip file and decompress it
 
Try to decompress the zip from the http stream. This is just not working
"""
 
  o = urlparse(url)
  conn = HTTPConnection(o.netloc)
  conn.request("GET", o.path)
  resp = conn.getresponse()
 
  data = resp.read()
 
  with open(filename, "w") as f:
    f.write(zlib.decompress(data))
 
 
if __name__ == "__main__":
  url = "http://shifteleven.com/dummy.txt.gz"
  workding_download_gzip_file(url, "working.txt")
  nonworkding_download_gzip_file(url, "nonworking.txt")