Cancel downloading file, if takes longer than 5 sec

I need to download file thru proxy. Sometimes proxy dont work at all so it takes some time until script timeouts. How i can cansel it if downloading takes longer than 5 seconds?

Not sure what you want to do cirno. I’m guessing that you want to abort the download if if fails to start in five seconds, not if it actually takes more than 5 seconds to complete.

In any case, we can’t help without knowing what to test for, and you haven’t told us how or what you’re downloading, and what happens to the downloaded material. Is it a web page or file, for example? What app is doing it?

maybe the shell command ‘curl’ is useful in your case?
It has options for proxy use and different options for timeout - see ‘man curl’ for details:

http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/curl.1.html

Yes, i want to abort download if it fails to start in five seconds. I try to download big html-file thru proxy to desktop using AppleScript with curl or whatever. I dont know unix enough to create correct curl… command.

Is this correct:
curl --connect-timeout 5 -m/–max-time 100 --proxy-basic

If i use this code, does it use system proxy settings?

It uses what you tell it to use. In AppleScript, the call would look like this (but I can’t test it for you):


set theDoc to (do shell script "curl --connect-timeout 5 -m/--max-time 100 --proxy-basic http://address.goesHere.com")

See also: -E/–cert, -F, -u/–user username:password , -U/–proxy-user username:password, if a username and password are required. The last of these seems most likely.

Hi cirno,

from your description I guess you want to try different proxies for your downloads? So I suggest using curl with this options:

curl -x196.211.107.74:8080 --connect-timeout 5 -O http://images.apple.com/home/2006/images/macprohero20060807.jpg

where ‘196.211.107.74:8080’ is the proxy address, ‘5’ the number of seconds before connection time out and ‘http://images.apple.com/home/2006/images/macprohero20060807.jpg’ the address of the file you want to download.
‘-O’ tells curl to download in the current folder and use the original file name for saving (so you might want to make a ‘cd destination folder’ before)

so used in AppleScript your script could look like so:

property proxyList : {"218.209.219.84:8080", "220.76.64.205:8080", "218.11.207.244:80", "212.162.158.82:8080", "203.229.170.234:8080", "220.77.57.51:8080", "210.233.102.69:8080", "210.233.102.68:8080", "212.162.158.91:8080", "210.233.102.66:8080", "210.233.102.70:8080", "220.89.82.152:8080", "211.226.124.209:8080", "61.185.219.235:80", "44.21.broadband3.iol.cz:8080", "62.183.25.238:3128", "129.41.250.20:80", "220.122.22.236:8080", "210.17.38.34:80", "212.93.193.72:80"}

property maxDownloadTime : 120
property maxConnectTime : 5

set downloadDestination to "~/Desktop/"
set downloadSource to "http://images.apple.com/home/2006/images/macprohero20060807.jpg"
set downloadSuccess to false

repeat with theProxy in proxyList
	
	with timeout of maxDownloadTime seconds
		try
			do shell script "cd " & downloadDestination & "; curl -x" & theProxy & " --connect-timeout " & (maxConnectTime as string) & " -O " & downloadSource
			set downloadSuccess to true
			exit repeat
		on error errmsg
			if (errmsg does not contain "curl: (28) connect() timed out!") then
				display dialog errmsg
			end if
		end try
		
	end timeout
end repeat

if downloadSuccess then
	display dialog "your file was successfully downloaded using proxy: " & theProxy
else
	display dialog "sorry, no success"
end if

D.