LUPA首页 | 资讯 | 教程 | 下载 | 求职 | 方案 | 博客 | 交易 | 英文版
LUPA论坛


 
标题: 用Erlang实现一个简单的Web服务器
henry.ning
版主
Rank: 7Rank: 7Rank: 7
果然很寂寞


风雨同舟奖  
UID 24562
精华 6
积分 892
帖子 361
LUPA币 699 点
阅读权限 100
注册 2006-9-9
来自 20644931
发表于 2006-10-3 08:58  资料 个人空间 短消息 
用Erlang实现一个简单的Web服务器

  用Erlang实现一个简单的Web服务器

新建一个httpd.erl文件,内容如下,

%% httpd.erl - MicroHttpd
-module(httpd).
-author("ninhenry@gmail.com").

-export([start/0,start/1,start/2,process/2]).
-import(regexp,[split/2]).

-define(defPort,6888).
-define(docRoot,"public").

start() -> start(?defPort,?docRoot).
start(Port) -> start(Port,?docRoot).  
start(Port,DocRoot) ->
   case gen_tcp:listen(Port, [binary,{packet, 0},{active, false}]) of
       {ok, LSock}        -> server_loop(LSock,DocRoot);
       {error, Reason}        -> exit({Port,Reason})
   end.

%% main server loop - wait for next connection, spawn child to process it
server_loop(LSock,DocRoot) ->
   case gen_tcp:accept(LSock) of
       {ok, Sock}        ->
           spawn(?MODULE,process,[Sock,DocRoot]),
           server_loop(LSock,DocRoot);
       {error, Reason}        ->
           exit({accept,Reason})
   end.

%% process current connection
process(Sock,DocRoot) ->
   Req = do_recv(Sock),
   {ok,[Cmd|[Name|[Vers|_]]]} = split(Req,"[ \r\n]"),
   FileName = DocRoot ++ Name,
   LogReq = Cmd ++ " " ++ Name ++ " " ++ Vers,
   Resp = case file:read_file(FileName) of
       {ok, Data}        ->
           io:format("~p ~p ok~n",[LogReq,FileName]),
           Data;
       {error, Reason}        ->
           io:format("~p ~p failed ~p~n",[LogReq,FileName,Reason]),
           error_response(LogReq,file:format_error(Reason))
   end,
   do_send(Sock,Resp),
   gen_tcp:close(Sock).

%% construct HTML for failure message
error_response(LogReq,Reason) ->
   "<html><head><title>Request Failed</title></head><body>\n" ++
   "<h1>Request Failed</h1>\n" ++ "Your request to " ++ LogReq ++
   " failed due to: " ++ Reason ++ "\n</body></html>\n".

%% send a line of text to the socket
do_send(Sock,Msg) ->
   case gen_tcp:send(Sock, Msg) of
       ok                -> ok;
       {error, Reason}        -> exit(Reason)
   end.

%% receive data from the socket
do_recv(Sock) ->
   case gen_tcp:recv(Sock, 0) of
       {ok, Bin}        -> binary_to_list(Bin);
       {error, closed}        -> exit(closed);
       {error, Reason}        -> exit(Reason)
   end.

在当前目录新建一个public目录,里面方个index.html,内容为
Hello world.

启动erl,运行"c(httpd)."
然后退出erl,重新进入erl,运行"httpd:start()."
访问http://localhost:6888/index.html

OK,一个简单的Web服务器完成了,这个功能还十分简单。。。

原帖:
http://cn.udclub.com/forum/viewt ... &extra=page%3D1




*
* WELCOME to the VeriSign Global Registry Service Whois Server.
*
* Sorry, the Whois database is currently down.
*
* Please wait a while and try again.  Thanks
*
顶部
[广告] 推荐个超酷的web2.0相册
henry.ning
版主
Rank: 7Rank: 7Rank: 7
果然很寂寞


风雨同舟奖  
UID 24562
精华 6
积分 892
帖子 361
LUPA币 699 点
阅读权限 100
注册 2006-9-9
来自 20644931
发表于 2006-10-3 08:58  资料 个人空间 短消息 
源代码下载


附件: Fid_254/254_3784.gz (2006-10-3 08:58, 3 K)
该附件被下载次数 240




*
* WELCOME to the VeriSign Global Registry Service Whois Server.
*
* Sorry, the Whois database is currently down.
*
* Please wait a while and try again.  Thanks
*
顶部
[广告] 推荐个超酷的web2.0相册
红色小魔鬼
版主
Rank: 7Rank: 7Rank: 7
阳光爱心


风雨同舟奖  
UID 9403
精华 5
积分 682
帖子 734
LUPA币 305 点
阅读权限 100
注册 2005-10-24
发表于 2006-10-3 12:48  资料 文集 短消息  QQ
呵呵,这里也有人玩erlang啊。不错不错。




E-mail: hhding!!gnu!gmail!!com
顶部
[广告] 推荐个超酷的web2.0相册
lch21
初来乍到
Rank: 1



UID 30101
精华 0
积分 12
帖子 1
LUPA币 11 点
阅读权限 10
注册 2007-1-24
发表于 2007-4-2 19:33  资料 短消息 
下载了,可用,很不错!
写得相当简洁
顶部
dunne
关注开源
Rank: 2



UID 38426
精华 0
积分 83
帖子 6
LUPA币 80 点
阅读权限 20
注册 2007-4-11
发表于 2007-4-11 17:43  资料 短消息 
kangaroo-egg(袋鼠蛋)是国内第一个开源的动态web服务器,是完全采用java技术开发的功能强大拥有完全自主知识产权且开源的web服务器。其拥有自己的开发语言DQM及容器(类似于Servlet/JSP),可以很容易开发出满足各种业务要求的web应用。
同时具有:
动态网页扩展名自定义;
集成HTTP压缩功能;
自动生成和管理静态页面;
有条件的文件输出(下载);
隐藏动态网页代码;
----------------------
http://www.kangaroo-egg.com
顶部
ccppasm2
关注开源
Rank: 2



UID 39187
精华 0
积分 21
帖子 1
LUPA币 20 点
阅读权限 20
注册 2007-4-15
发表于 2007-4-15 20:33  资料 短消息 
ttttttttttttt
顶部
taoyh
关注开源
Rank: 2



UID 18417
精华 0
积分 55
帖子 6
LUPA币 52 点
阅读权限 20
注册 2006-4-3
发表于 2007-4-17 18:47  资料 主页 短消息 
看到大熊猫了.这个东西是专门函数编程的动态脚本语言.wings3D,就是用它的.




zope/plone爱好者。
顶部
talons
开源主力军
Rank: 3Rank: 3


UID 28903
精华 0
积分 566
帖子 50
LUPA币 541 点
阅读权限 30
注册 2007-1-8
发表于 2007-8-27 17:35  资料 短消息 
下个看看............




http://www.talons.cn
顶部
 


当前时区 GMT+8, 现在时间是 2008-8-8 04:03
浙ICP备06002895号

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.048053 second(s), 8 queries , Gzip enabled

清除 Cookies - 联系我们 - LUPA开源社区 - Archiver - WAP