URL の内容をファイルに保存したいだけなら、標準http
パッケージには-channel
直接ダンプできるオプションがあります。例えば:
package require http
set f [open video.dump w]
fconfigure $f -translation binary
set tok [http::geturl "http://server:port/url" -channel $f]
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
編集:非同期で行う(イベントループが必要です。たとえば、を介してvwait forever
):
package require http
set f [open video.dump w]
fconfigure $f -translation binary
proc done {f tok} {
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
}
http::geturl "http://server:port/url" -channel $f -command "done $f"
# Your code runs here straight away...
コードは認識できるほど似ていますが、順序が少し異なることに注意してください。Tcl 8.5 を持っている場合 — そうでない場合は、なぜですか? — 次に、代わりにラムダ アプリケーションを使用して、コードの見かけ上の順序をさらに似たものにすることができます。
package require http
set f [open video.dump w]
fconfigure $f -translation binary
http::geturl "http://server:port/url" -channel $f -command [list apply {{f tok} {
close $f
if {[http::ncode $tok] != 200} {
# failed somehow...
} else {
# succeeded
}
http::cleanup $tok
}} $f]
# Your code runs here straight away...