As the title says... Chat on...

User avatar
By scargill
#3605 I'm new to LUA so this is giving me grief

sk=net.createConnection(net.TCP, 0)
sk:dns("www.bbc.co.uk",function(conn,ip) print(ip) end)
sk = nil

That's all very nice- it returns the IP address.. lovely... except that on it's own it's useless. I want to be creating a connection to go get something from a webserver...

Let's say I had a web page (public) from which I wanted to return data...

The ideal function would be returnstring=GetFromWebsite("www.bbc.co.uk", 80)

ie return the text that comes from the BBC website home page - ok - I know that's WAY TOO MUCH - but I have for example a web page that returns a small amount of JSON. I don't want to put the IP address in - for many websites that's unpredictable.

So when creating the connection, I need to use this DNS function to turn the website address into an IP address which is then used in the rest of the code.

Anyone any ideas how to put that first bit of code into a function that is blocking and simply returns the IP address (or an error msg) ??

Pete.
User avatar
By gerardwr
#3613 In my IOT/WEB example I use the function below, it just prints the entire reply from the webserver:
Code: Select all-- function httpget(ip,page)
-- A simple http client
-- Usage : httpget("192.168.0.20","index.html")
function httpget(ip,page)
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c) print(c) end )
    sk:connect(80,ip)
    sk:send("GET /" .. page .. " HTTP/1.1\r\nHost: ip" .. "\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
    sk:on("sent",function(sk) sk:close() end)
end


That worked for me, knowing the IP address. Also the call to the IP number of google.com worked (can't remember it now). Have not use the DNS call yet.

Maybe this helps.
User avatar
By scargill
#3622 Thanks but no :-) The point was that no-one wants to put IP addresses of external sites in - we just want to put in the address.. but thinking about it if the call to get the website info was put inside the DNS call back - maybe that would work - hmmm - right - I'm off to experiment.
User avatar
By scargill
#3627 Your example as shown here..


sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:connect(80,"192.168.0.66")
sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

That can't work on ANY external site.. it's working on internal sites - because you could put that IP address into your browser. But external sites most of the time SHARE an IP address. Something in that code needs to have the actual name of the site - and given that you need that information, hence my failed attempt to combine this with the DNS lookup - you need both the IP address AND the actual address. i.e. you need to add the HOST - so for example your line

sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")

becomes

sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nHost: www.google.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")


So

Here is my attempt to put the two together - get the DNS address then call the web page and return the results (dummy website address and page here) - sadly - it fails miserably.


myhost="www.yoursite.comt"
mypage="index.html"
myip=""

sk=net.createConnection(net.TCP, 0)
sk:dns(myhost,function(conn,ip)
myip=ip
sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:connect(80,myip)
sk:send("GET / " .. mypage .." HTTP/1.1\r\nHost: " .. myhost .."\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
sk=nil
end)
end