Receive attachment with urllib - Python
Question: I am testing my webpage software by sending requests from python to it. I am
able to send requests, receive responses and parse the json. However, one
option on the webpage is to download files. I send the download request and
can confirm that the response headers contain what I expect
(application/octet-stream and the appropriate filename) but the Content-Length
is 0. If the length is 0, I assume the file was not actually sent. I am able
to download files from other means so I know my software works but I am having
trouble with getting it to work with python.

I build up the request then do:

    
    
    f = urllib.request.urlopen(request)
    f.body = f.read()
    

I expect data to be in f.body but it is empty (I see "b''")

Is there a different way to access the file contents from an attachment in
python?


Answer: > Is there a different way to access the file contents from an attachment in
> python?

This is in [python-requests](http://docs.python-requests.org/en/latest/)
instead `urllib`, since I'm more familiar with that.

    
    
    import requests
    
    url = "http://www.mysite.com/totallynotavirus.jpg.exe"
    
    #make request
    r = requests.get(url)
    attachment_data = r.content
    
    #save to file
    with open(r"C:/system/win32/funnypictures", 'wb') as f:
      f.write(attachment)
    

