Super-mini web server that I use as a demonstration for presentations. Not correct or complete, but it does serve files...
It can also reload its own code if you hit it at the /reload url!
proc Serve {chan addr port} { fconfigure $chan -translation auto -buffering line set line [gets $chan] set path [file join . [string trimleft [lindex $line 1] /]] if { $path == "." } {set path ./index.html} if { $path == "./reload" } { set reload 1 source [info script] puts $chan "HTTP/1.0 200 OK" puts $chan "Content-Type: text/html" puts $chan "" puts $chan "Server reloaded" } else { #puts "Request: $path" ;## if { [catch { set fl [open $path] } err] } { puts $chan "HTTP/1.0 404 Not Found" } else { puts $chan "HTTP/1.0 200 OK" puts $chan "Content-Type: text/html" puts $chan "" puts $chan [read $fl] close $fl } } close $chan } #catch {console show} ;## if { ! [info exists reload] } { set sk [socket -server Serve 5151] vwait forever } else { unset reload }
Point your browser to http://localhost:5151
HJG Added "index.html" as default-url.
slebetman Whoa! Smaller than the DustMote and has the extra feature of being able to re-source itself! Impressive.
escargo 30 Nov 2006 - Won't this break if the input line can't be indexed with lindex?
MG guesses that it may need to be
set line [split [gets $chan] " "]
to make sure it's parsed properly as a list.
See also:
Category Application | Category Internet | Category Webserver