nim ndns
v0.1.3
使用 dnsprotocol 實現的純 Nim 網域名稱系統 (DNS) 用戶端。
此實作具有同步和非同步 (async) 流程 (procs),用於使用 UDP 和 TCP 協定透過網際網路傳輸資料。
nimble install ndns
或者
nimble install https://github.com/rockcavera/nim-ndns.git
解析 nim-lang.org 的 IPv4 位址(不是 async ):
import ndns
let client = initDnsClient ()
echo resolveIpv4 (client, " nim-lang.org " )
解析 nim-lang.org 的 IPv4 位址(非同步):
import asyncdispatch, ndns
let client = initDnsClient ()
echo waitFor asyncResolveIpv4 (client, " nim-lang.org " )
有關「現實生活」非同步範例,請參閱resolver.nim。在這個例子中,我做了盡可能多的評論,即使它們看起來很愚蠢。我認為這可能會對某人有所幫助,就像我私下為 Nim 的新人提供的一個類似例子一樣。它也可以使用-d:showLoopLog
進行編譯以顯示非同步工作流程。
使用QType.A
查詢網域 nim-lang.org 建立Message
對象,傳輸Message
並接收回應(非非同步):
import ndns
let header = initHeader ( randId (), rd = true )
let question = initQuestion ( " nim-lang.org " , QType .A, QClass . IN )
# If the last character of "nim-lang.org" is not a '.', the initializer will
# add, as it is called the DNS root.
let msg = initMessage (header, @ [question])
# The initializer automatically changes `header.qdcount` to `1'u16`
let client = initDnsClient ()
var rmsg = dnsQuery (client, msg)
echo repr (rmsg)
使用QType.A
查詢網域 nim-lang.org 建立Message
對象,傳輸Message
並接收回應(非同步):
import asyncdispatch, ndns
let header = initHeader ( randId (), rd = true )
let question = initQuestion ( " nim-lang.org " , QType .A, QClass . IN )
# If the last character of "nim-lang.org" is not a '.', the initializer will
# add, as it is called the DNS root.
let msg = initMessage (header, @ [question])
# The initializer automatically changes `header.qdcount` to `1'u16`
let client = initDnsClient ()
var rmsg = waitFor dnsAsyncQuery (client, msg)
echo repr (rmsg)
您可以使用系統使用的 DNS 解析伺服器來初始化 DNS 用戶端。為此,請使用initSystemDnsClient
啟動客戶端。
import ndns
let client = initSystemDnsClient ()
echo resolveIpv4 (client, " nim-lang.org " )
https://rockcavera.github.io/nim-ndns/ndns.html