如果您只是想將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 - 如果沒有,爲什麼不呢? - 那麼您可以使用lambda應用程序來代替代碼的明顯順序:
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...
VLC可能會緩衝。嘗試讀取1024字節(或1024的倍數)。 – 2010-07-21 15:39:39
我試過你的解決方案...我仍然有同樣的問題... – geniecom 2010-07-22 09:27:18