开发者

I want the actual file name that is returned by a PHP script

开发者 https://www.devze.com 2022-12-27 20:59 出处:网络
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

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 thi开发者_JS百科s 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号