1

I guess this is kind of a stupid question, but I have tried to search for the answere on google and here with no luck.

So I am connection to a API using a Get request with the http.client package.

The get request is sent to a adress with a ziped file. Sending the request goes ok, and I get a returned object. What I want to do next is to just save the returned file to a folder.

I have tried looking in the http.client documentation withount finding anything about it.

This is an example of how the code looks like. I have had to remove some of it for privacy reasons

conn = http.client.HTTPConnection("Adress to the server")

headers = {
    'cache-control': "no-cache",
    }

conn.request("GET", path_to_file + "?authTicket=" + ticket, 
headers=headers)

res = conn.getresponse()

return res

The code runs ok, but my problem is what to do next. If the returned data was just a normal json file I would just do something like:

data = res.read()
data = data.decode("uft-8")

And then be able to work with it inside the script. But I have no idea how to handel the ziped file that is returned. I would idealy like to unzip it before saving, but as a start it is ok to just save it to file.

Hope someone have a simple solution to this.

I did look a bit more into it, and when using the getheaders() on the returned object I get this:

[('Date', 'Wed, 19 Apr 2017 13:48:41 GMT'),
('Content-Type', 'application/octet-stream; charset=UTF-8'),
('Content-Disposition', 'attachment;filename=filename.csv.gz'),
('Content-Length', '2117014'),
('Server', 'Jetty(9.2.17.v20160517)')]

im not sure if that is relevant, but I guess I now would need a way to download the attachement.

Siesta
  • 451
  • 7
  • 21

1 Answers1

0

You can extract a zip file easily in Python:

import requests
from zipfile import ZipFile
import io

my_request = requests.get(URL)
ficheiro = ZipFile(io.BytesIO(my_request.content))

ficheiro.extract('file_name')

You can get more info about the zipfile module and the zipfile.ZipFile class here:

https://docs.python.org/3/library/zipfile.html

Victor Domingos
  • 1,003
  • 1
  • 18
  • 40
  • You can see this in action, for instance, in my project PT-Tracking for iPhone/Pythonista, where the iPhone gets a zipped subset of a sqlite database for viewing and/or further offline processing: https://github.com/victordomingos/PT-Tracking/tree/master/ctt_tracking_iPhone – Victor Domingos Apr 19 '17 at 14:13
  • ok, so it seems like it should be ok to extract the file, but do you have any tips on how I gets the file from the returned object? In your example code you just pass in an URL to th e request package. How do I access the filename=filename.csv.gz – Siesta Apr 19 '17 at 14:21
  • You can extract directly from memory right after it is received, as long as it is not a huge file and it fits the computer RAM. But it is also pretty simple to open a previously saved zip or gzip file for the extraction. See: https://docs.python.org/3.6/library/zipfile.html#zipfile.ZipFile.open – Victor Domingos Apr 19 '17 at 14:29
  • Notice however that if the file is compressed in gzip format, you should opt for the gzip module instead: https://docs.python.org/3.6/library/gzip.html?highlight=gzip#module-gzip – Victor Domingos Apr 19 '17 at 14:30
  • Hi, @Siesta! If this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Victor Domingos Oct 12 '18 at 10:06