tags:

views:

335

answers:

1

I need a little help getting a tar file to download from a website. The website is set up as a form where you pick the file you want and click submit and then the download windows opens up for you to pick the location.

I'm trying to do the same thing in code (so I don't have to manual pick each file). So far I have gotten python 2.5.2 to return a response but its in a socket._fileobject and I'm now not sure how to convert that into a file on my computer.

Below is output of the python shell and the steps I have taken

Python 2.5.2 on win32

IDLE 1.2.2      
>>> import urllib
>>> import urllib2
>>> url = 'http://website/getdata.php'
>>> values = {'data[]': 'filex.tar'}
>>> data = urllib.urlencode(values)
>>> req = urllib2.Request(url,data)
>>> response = urllib2.urlopen(req)
>>> response
addinfourl at 22250280 whose fp = <socket._fileobject object at 0x01534570
>>> response.fp
socket._fileobject object at 0x01534570
+2  A: 

Append this:

myfile = open('myfile.tar', 'wb')
shutil.copyfileobj(response.fp, myfile)
myfile.close()

response.fp is a file-like object that you can read from, just like an open file. shutil.copyfileobj() is a simple function that reads from one file-like object and writes its contents to another.

RichieHindle
You can copy the response.fp in any way you like, but shutil is no doubt the best for simplicity and reuse of well-tested code.
Alex Martelli
This solves the problem Thanks Richie for your help.