Version 0 of Starting Tclhttpd in a slave interpreter

Updated 2003-04-10 21:58:00

One simple way to embed tclhttpd into an tcl application is to just create a slave interpreter and load tclhttpd inside the slave. This is easy and works quite well.

  # example code
  proc StartEmbeddedHttpd {path args} {

    set httpd [interp create] 

    # set tclhttpd command line options
    $httpd eval "set argc [llength $args]"
    set cmdargv "set argv [list $args ]"
    $httpd eval $cmdargv

    # now load the server and start it
    set cmd [list source $path]

    # the server does not return, so we have to use the after 0 trick
    # errors have to be handled by bgerror

    after 0 $httpd eval $cmd

    return $httpd
  } 

  proc ShutdownEmbeddedHttpd {interp} {
    $interp eval Httpd_Shutdown
  }

  proc DestroyEmbeddedHttpd {interp} {
    $interp delete
  }