金狮镖局 Design By www.egabc.com
转贴一个简单的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,8888). -define(docRoot,"public"). start() -> start("[ \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
运行时在httpd.erl本地建一个public目录,public目录里放一个index.html文件
然后httpd:start()启动服务器,就可以访问http://localhost:8888/index.html了
标签:
Erlang,Web,服务器
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无Erlang实现的一个Web服务器代码实例的评论...
更新日志
2024年05月20日
2024年05月20日
- 林志颖.1994-火热的心【飞碟】【WAV+CUE】
- 半糖初恋
- 大脚野人日落 Sasquatch Sunset
- 独角兽塞尔玛 Thelma the Unicorn
- 群星《世界顶级汽车音响试音王(嗨歌王)24K金碟》16CD[WAV+CUE][7.9G]
- 《曾宝仪4CD合集》[WAV+CUE][2.3G]
- 周杰伦《八度空间》台湾手版[WAV+CUE][475M]
- 咏月风雅前期怎么玩 前期组队玩法解析
- 咏月风雅攻略大全 新手入门必看攻略
- 咏月风雅公测福利大全 有哪些奖励
- dnf装备成长攻略
- 鸣潮丹瑾适配武器选什么 丹瑾四星五星武器推荐
- dnf装备指令输入怎么输入
- 《对马岛之魂》超越《战神4》成为PC最火爆PS游戏!
- 《生化1 重制版》首批细节曝光!击杀敌人要更多子弹