Lua套接字示例,尝试不同的网站

时间:2014-10-02 16:12:22

标签: lua

我正在使用这个例子。它会根据此网站http://www.google.com/robots.txt

打印文本
local socket = require("socket")
client = socket.connect("google.com", 80)
client:send("GET /robots.txt HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive(1024)
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()

我用:

local socket = require("socket")
client = socket.connect("www.lua.org", 80)
client:send("GET /pil/9.4.html HTTP/1.0\r\n\r\n")
while true do
  s, status, partial = client:receive(1024)
  print(s or partial)
  if status == "closed" then 
    break 
  end
end
client:close()

但是,如上所述,我尝试使用此链接http://www.lua.org/pil/9.4.html并且它不起作用,说" HTTP / 1.0 302暂时移动"。在许多其他网站上也是如此,得到了类似的结果。为什么会这样?非常感谢

1 个答案:

答案 0 :(得分:0)

您正在接收重定向(HTTP 302代码),应通过向指定的Location发出新请求来处理。

您可以使用套接字库中的高级HTTP模块,而不是使用原始套接字,该模块提供了一种能够自动跟踪重定向的request方法。