首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Erlang inets httpc连接到本地inets httpd

Erlang inets httpc连接到本地inets httpd
EN

Stack Overflow用户
提问于 2015-05-08 12:34:05
回答 1查看 678关注 0票数 0

我试图用httpd实现一个模块,它处理进程的HTTP请求。基本上,它处理传入的get请求,解析get参数,并在节点上调用同步gen_server方法,该方法启动了http服务器。然后,它使用同步调用的返回值返回一些HTML。所有这些都很好,直到我用浏览器(Chrome,Firefox最新版本起作用)进行测试。但是我需要实现HTTP转发功能,其中http服务器可以用给定的参数调用另一个URL。我用httpc尝试过:

代码语言:javascript
复制
httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).

但是引发了以下错误:

代码语言:javascript
复制
{error,{failed_connect,[{to_address,{"localhost",55556}},
                        {inet,[inet],econnrefused}]}}

你能帮帮我吗?我遗漏了什么?

Ps。其他站点,比如stackoverflow.com,可以使用上面的httpc:request(...)

Config:我启动node.erl,节点在另一个模块中启动httpfront.erl httpd守护进程,get请求在http前沿处理。我做了几次(启动更多的节点),现在我想用httpc:request(.)从一个http前沿实例连接另一个。所有的HTTP处理程序都是在本地主机上启动的,但是不同的非专有端口。

node.erl:

代码语言:javascript
复制
http_listener() ->
    httpfront:start().

start() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
...

httpfront.erl:

代码语言:javascript
复制
start() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []),
%this is the port number, it increments on each new node
    P = 55556,
    % the corresponding url is: http://localhost:port/
    % command are accesible this way:
    % port is from 55556 until end of valid port range, increments on new node join
    % http://localhost:55556/httpfront:get?key=thisisashortkey
    inets:start(),
    inets:start(httpd, [
        {modules, [
            mod_alias, 
            mod_auth, 
            mod_esi, 
            mod_actions, 
            mod_cgi, 
            mod_dir, 
            mod_get, 
            mod_head, 
            mod_log, 
            mod_disk_log
        ]},
        {port,P},
        {server_name,"httpfront"},
        {server_root,"log"},
        {document_root,"www"},
        {erl_script_alias, {"", [httpfront]}},
        {error_log, "error.log"},
        {security_log, "security.log"},
        {transfer_log, "transfer.log"},
        {mime_types,[
            {"html","text/html"}
            ]}
        ]),
        io:format("OK. Good to go.~n").

    get(SessionID, _Env, Input) ->
    % this get() is a gen_server synchronous call on the inside
    Result = node:get(Input),

    mod_esi:deliver(SessionID, [
        "Content-Type: text/html\r\n\r\n", 
        "<html><body>",
        "Get<br>",
        "Key=" ++ Value,
        "<br>Result=" ++ Result,
        "</body></html>"


    ]).
    ...

最后,我尝试从另一个节点调用它,正如我所说的,但是我得到了错误:

代码语言:javascript
复制
httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).
{error,{failed_connect,[{to_address,{"localhost",55556}},
                        {inet,[inet],econnrefused}]}}

如有任何帮助和建议,将不胜感激。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-09 11:35:35

尝试将{ipfamily, inet}作为httpd开始调用的选项列表的成员传递,以便让它在IPv4上而不是IPv6上侦听。另外,由于您可能只需要localhost连接,所以也可以考虑传递{bind_address, {127,0,0,1}}选项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30124008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档