File: http_get_file.tcl

package info (click to toggle)
tcltls 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: ansic: 4,349; tcl: 1,392; sh: 408; makefile: 32
file content (30 lines) | stat: -rw-r--r-- 736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#################################################
#
# Download file using HTTP package
#
#################################################

package require Tcl 8.6-
package require tls
package require http

set url "https://wiki.tcl-lang.org/sitemap.xml"
set protocol "http/1.1"
set filename [file tail $url]

# Register https protocol handler with http package
http::register https 443 [list ::tls::socket -autoservername 1 -require 1 -alpn [list [string tolower $protocol]]]

# Open output file
set ch [open $filename wb]

# Get webpage
set token [::http::geturl $url -blocksize 16384 -channel $ch]
if {[http::status $token] ne "ok"} {
    puts [format "Error %s" [http::status $token]]
}

# Cleanup
::http::cleanup $token
close $ch