Erlang inets httpc 连接本地inets httpd
Erlang inets httpc connect to local inets httpd
我正在尝试使用 httpd 实现一个模块,它处理进程的 HTTP get 请求。基本上它处理传入的 get 请求,解析 get 参数并在启动 http 服务器的节点上调用同步 gen_server 方法。然后,它 returns 一些 HTML 与来自同步调用的 return 值。一切正常,直到我用浏览器测试它(Chrome,Firefox 最新版本工作)。但是我需要实现一个 HTTP 转发功能,其中 http 服务器可以使用给定的参数调用另一个 URL。我用 httpc:
试过了
httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).
但抛出以下错误:
{error,{failed_connect,[{to_address,{"localhost",55556}},
{inet,[inet],econnrefused}]}}
你能帮帮我吗?我错过了什么?
Ps。其他站点,例如 whosebug.com
与上述 httpc:request(...)
.
一起使用
配置:
我启动 node.erl,节点在另一个模块中启动 httpfront.erl httpd 守护进程,get 请求在 httpfront 处理。我这样做了好几次(启动更多节点),现在我想从一个带有 httpc:request(...) 的 httpfront 实例连接到另一个。所有 HTTP 处理程序都在本地主机启动,但不同的非专有端口。
node.erl:
http_listener() ->
httpfront:start().
start() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
...
httpfront.erl:
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>"
]).
...
现在我终于尝试从另一个节点调用它,正如我所说的那样,但我得到了错误:
httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).
{error,{failed_connect,[{to_address,{"localhost",55556}},
{inet,[inet],econnrefused}]}}
感谢任何帮助和建议。谢谢。
尝试将 {ipfamily, inet}
作为选项列表的成员传递给您的 httpd
开始调用,以使其侦听 IPv4 而不是 IPv6。此外,由于您可能只需要 localhost
连接,因此您也可以考虑传递 {bind_address, {127,0,0,1}}
选项。
我正在尝试使用 httpd 实现一个模块,它处理进程的 HTTP get 请求。基本上它处理传入的 get 请求,解析 get 参数并在启动 http 服务器的节点上调用同步 gen_server 方法。然后,它 returns 一些 HTML 与来自同步调用的 return 值。一切正常,直到我用浏览器测试它(Chrome,Firefox 最新版本工作)。但是我需要实现一个 HTTP 转发功能,其中 http 服务器可以使用给定的参数调用另一个 URL。我用 httpc:
试过了httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).
但抛出以下错误:
{error,{failed_connect,[{to_address,{"localhost",55556}},
{inet,[inet],econnrefused}]}}
你能帮帮我吗?我错过了什么?
Ps。其他站点,例如 whosebug.com
与上述 httpc:request(...)
.
配置: 我启动 node.erl,节点在另一个模块中启动 httpfront.erl httpd 守护进程,get 请求在 httpfront 处理。我这样做了好几次(启动更多节点),现在我想从一个带有 httpc:request(...) 的 httpfront 实例连接到另一个。所有 HTTP 处理程序都在本地主机启动,但不同的非专有端口。
node.erl:
http_listener() ->
httpfront:start().
start() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
...
httpfront.erl:
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>"
]).
...
现在我终于尝试从另一个节点调用它,正如我所说的那样,但我得到了错误:
httpc:request(get, {"http://localhost:55556/httpfront:get?key=1", []}, [], []).
{error,{failed_connect,[{to_address,{"localhost",55556}},
{inet,[inet],econnrefused}]}}
感谢任何帮助和建议。谢谢。
尝试将 {ipfamily, inet}
作为选项列表的成员传递给您的 httpd
开始调用,以使其侦听 IPv4 而不是 IPv6。此外,由于您可能只需要 localhost
连接,因此您也可以考虑传递 {bind_address, {127,0,0,1}}
选项。