Vaibhav Aggarwal
Vaibhav Aggarwal

Reputation: 1411

How do i check if a file is uploaded on a website with python

I have a website from which i want to download files. A new file is uploaded about every other day. How can i check if a new file is up or not?

Ex: url1 = website.com/file_2013-06-27.zip <-- uploaded

url2 = website.com/file_2013-06-29.zip <-- not uploaded

if i go to url 2, in 5 seconds it redirects back to website.com the source code of it is: <meta http-equiv="refresh" content="5;url=http://website.com" /> Error: 2 [ Not Allowed ]

The size of the files are 100mb+ and if i try to look at the source by doing urllib.urlopen("website.com/file_2013-06-27.zip").read(), it takes a while if the file exists.

Whats a quick way to check if a new file was uploaded?

Thanks

Upvotes: 1

Views: 3282

Answers (1)

Charles Newey
Charles Newey

Reputation: 345

Python's Requests library is great for checking things like HTTP status codes (not downloading files, just getting the response)

For example:

import requests

r = requests.get('website.com/file_2013-06-27.zip')
if r.status_code == 200:
    print ("File uploaded.")

That doesn't download the file (just tried it with a 1GB file), just checks if the web server will serve it and what the HTTP response is. With HTTP, 200 means that the file exists, and is accessible. See below for more info on HTTP response codes.

More info:
http://docs.python-requests.org/en/latest/ - the requests library
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html - guide to HTTP response codes

Upvotes: 1

Related Questions