[Answered ]-Checking if a URL exists and is smaller than x bytes without consuming full response

2👍

✅

You could open the URL in urllib and read(x+1) from the returned object. If the length of the returned string is x+1, then the resource is larger than x. Then call close() on the object to close the connection, i.e. kill the request. In the worst case, this will fill the OS’s TCP buffer, which is something you can not avoid anyway; usually, this should not fetch more than a few kB more than x.

If you furthermore add a Range header to the request, sane servers will close the connection themselves after x+1 bytes. Note that this changes the reply code to 206 Partial Content, or 416 Requested range not satisfiable if the file is too small. Servers which do not support this will ignore the header, so this should be a safe measure.

Leave a comment