Perl Web 服务器初级编程[7]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 nistal 的 blog

                  my $httpd= new http::daemon;

                  print “server running at :“,$httpd->url(),“\n“;
                  while(my $connection=$httpd->accept)
                  {
                         while(my $request=$connection->get_request)
                         {
                                 if($request->method eq 'get')
                                  {
                                      my $file=$request->url->path;
                                      $connection->send_file_response(“/$docroot/$file“);
                                  }
                                 else{
                                       $connection->send_error(rc_forbidden);
                                       }
                                   $connection->close;
                         }
                          undef($connection);
                  }
                 http::daemon对象从io::socket::inet模块继承,我们可以在其上进行socket操作。daemon对象扫描本地主机以获取一个可能的名字,并选择一个端口号服务 eg:在unix服务器上
                 server running at: http://localhost.localdomain:1640/
                 通过浏览器指向服务器发送简单请求后,创建daemon对象,并由其接收呼叫,等待连接,当客户机连接后,返回一个连接对象。检索将get请求翻译成一个路径的uri,最后我们在连接对象上调用send_file_response将请求文档回送客户机,if not found,发送404 not found响应,if 是目录,发送501 not implemented错误。

                 列举httpd::daemon的方法:
                        1.new  创建新服务器
                           eg: $httpd=new httpd:: daemon(

本文关键:Perl Web 服务器初级编程
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top