erlang實現DNS解析網域名稱

來源:互聯網
上載者:User

標籤:style   blog   http   color   width   os   

  erlang的DNS解析,有個inet模組,可以做這個事情,對應的api如下:(這裡用的R16B的版本)

inet:getaddr/2解析對應一個ip,而inet:getaddr/2解析有可能對應1個或多個映射的IP. 第一個參數為host網域名稱,第二個參數是ipv4或ipv6.

如:

  

 

簡單寫了下測試代碼,直接上代碼:

 

enter() ->        [ resolve(Host) || Host <- domains()].%%網域名稱列表domains() -> ["www.baidu.com","www.china.com",              "www.sohu.com","www.msn.com"].        resolve(Host) ->    %%DNS解析,通過網域名稱解析對應一個IP值    IP   = case inet:getaddr(Host,inet) of                {ok,{IP1,IP2,IP3,IP4}} -> combin_ip(IP1,IP2,IP3,IP4);                {error, Reason} -> throw(Reason)           end,    inets:start(),    httpc:request(IP).    %%拼接IPcombin_ip(IP1,IP2,IP3,IP4) ->        "http://" ++ integer_to_list(IP1) ++ "." ++ integer_to_list(IP2) ++         "." ++ integer_to_list(IP3) ++ "." ++ integer_to_list(IP4).

運行結果如下:

  

做了多個網域名稱的解析,解析成IP後,再用erlang內部封裝的inets模組(輕量級http的架構),去分別做http的請求.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.