我正在玩Nimrod,我尝试了HTTPServer模块。我首先想到它不起作用,因为当我试图在我的标准浏览器Safari中访问网页时,它给出了一个错误。有错误是这样的:(对不起,如果它不完全正确,它是从荷兰语翻译)
Safari无法打开此页面 Safari无法打开localhost:5000页面,因为网络连接意外丢失
但是,当我尝试在Chrome或Firefox中打开该页面时,它可以正常运行
我的Nimrod代码:
import strutils, sockets, httpserver
var counter = 0
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
inc(counter)
client.send("Hello for the $#th time." % $counter & wwwNL)
return false # do not stop processing
run(handleRequest, TPort(5000))
答案 0 :(得分:2)
似乎client.send直接写入线路 - 无需添加HTTP状态行或任何标头。您可以通过至少添加状态行来使其工作:
import strutils, sockets, httpserver
var counter = 0
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
inc(counter)
client.send("HTTP/1.0 200 OK\r\n\r\nHello for the $#th time." % $counter & wwwNL)
return false # do not stop processing
run(handleRequest, TPort(5000))