views:

76

answers:

2

I am writing a python script that downloads a file given by a URL. Unfortuneatly the URL is in the form of a PHP script i.e. www.website.com/generatefilename.php?file=5233

If you visit the link in a browser, you are prompted to download the actual file and extension. I need to send this link to the downloader, but I can't send the downloader the PHP link.

How would I get the full file name in a usable variable?

+2  A: 

What you need to do is examine the Content-Disposition header sent by the PHP script. it will look something like:

Content-Disposition: attachment; filename=theFilenameYouWant

As to how you actually examine that header it depends on the python code you're currently using to fetch the URL. If you post some code I'll be able to give a more detailed answer.

Josh
I am retrieving the PHP url from an RSS feed, and want to pass the file (and full path) generated by visiting the PHP URL into a variable, so that I can send it to a downloader application via a localhost url. Basically the PHP URL is given, and I have no code that handles the PHP
Aymon Fournier
Are you using Python to access the PHP script via HTTP?
Josh
I'm using python to parse XML and obtain the PHP Script's URL e.g. www.website.com/generatefilename.php?file=5233 and store it in a variable. What I want is to get the URL to the file name generated by the script. I have no idea how to do this. All I have so far is this URL to a PHP Script, and I know that if I visit it in Firefox, a download dialog comes up for the generated/retrieved file e.g. 5233Retrieved.dat and that's the file whose full path I want to store in a variable.
Aymon Fournier
If you use Python to fetch the `www.website.com/generatefilename.php?file=5233` URL, you can examine the HTTP headers and they will contain the filename. To authenticate when fetching content over HTTP using Python is a separate question.
Josh
A: 
urllib.urlretrieve(URL, directory + "\\" + filename + "." + extension)

This saved the file generated by the PHP file to the designated folder with the designated name and extension. I configured the downloader to automatically check this folder for new files, so this solution works for me.

Aymon Fournier
Nevermind, the files are all the same size and don't contain the correct data!
Aymon Fournier
Actually I renamed the files and found that they are webpages requiring authentication, so I don't think my script was authenticated before attempting to obtain the files. So it might work if I could just figure out how to authenticate with the website via URL, if thats possible.
Aymon Fournier
Ok, with authentication, this line retrieves the generated file.
Aymon Fournier