# set website "www.yourhost.com"
# set content [web2data $website]
# returns:
# - if no error: the source of the website
# - if error: 0
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
proc web2data { website } {
package require http
# send the http request, -timeout sets up a timeout to occur after the specified number of milliseconds
# we use catch to avoid an abort of the script in case of an error when executing http::geturl (e.g. due to an unsupported url)
if { [catch { set token [http::geturl $website -timeout 3000]} error] } {
putcmdlog "web2data: Error: $error"
# if the the site does not exist
} elseif { [http::ncode $token] == "404" } {
putcmdlog "web2data: Error: [http::code $token]"
# check if the request was successful, if yes -> put the html source code into $data
} elseif { [http::status $token] == "ok" } {
set data [http::data $token]
# if a timeout has occurred, send "Timeout occured" to the standad output device
} elseif { [http::status $token] == "timeout" } {
putcmdlog "web2data: Timeout occurred"
# send the error to the standard output device if there is one
} elseif { [http::status $token] == "error" } {
putcmdlog "web2data: Error: [http::error $token]"
}
# last but not least, release the memory which was used for these operations
http::cleanup $token
if { [info exists data] } {
return $data
} else {
return 0
}
}
|
# same like Mettwurst''s but as proc
|