Communicating with the Internet

Posted by Candido on Sat 24 Nov 2007 03:41 PM — 11 posts, 55,773 views.

USA #0
A while ago I converted my script from Python to Lua, all except for one thing I haven't been able to find a Lua equivalent for:

import urllib
urllib.urlopen("http://www.lusternia.com/vote")

Normally, you open that url in your browser and it votes for Lusternia on topmudsites.com.

I'm not completely sure how this bit of Python works, but when I run it while connected the game gives me a line thanking me for voting. How can I do this in Lua?
Amended on Sat 24 Nov 2007 08:48 PM by Candido
Australia Forum Administrator #1
See:

http://www.gammon.com.au/forum/bbshowpost.php?id=4935

My first post there shows a bit of Lua code that accesses a web page.
USA #2
I haven't been able to get this to work. The page you downloaded luasocket-2.0-beta2.tar.gz from seems to be gone, so I looked up luasocket on the Lua Forge.

First I tried the most recent version (luasocket-2.02), but the files were different and I couldn't successfully adapt them to the instructions you gave. Next I tried the oldest version they had (luasocket-2.0-beta3), and while it was more familiar, both the .zip and the .tar.gz were missing some files, notably lua.lua.

So, I did what I could. I copied tp.lua, url.lua, ftp.lua, http.lua, ltn12.lua, mime.lua, smtp.lua, socket.lua, lmime.dll, and lsocket.dll to what I think is the Mushclient directory (C:\Program Files\Mushclient).

Then I tried dofile("http.lua") and dofile("C:\Program Files\Mushclient\http.lua"), and both failed. Where did I go wrong?
Australia Forum Administrator #3
Er, yes it got a bit confusing. I took a look at my page here:

http://www.gammon.com.au/forum/?id=7322

That described the DLLs for use with Lua 5.1, which are different from the original post. Then after looking at the complicated instructions halfway down the page ("How to get LuaSocket to work"), and the fact that the LuaSocket file was not available from the advertised location, I simplified things a bit.

What I suggest you do now is:

  • Download this file from my site:

    http://www.gammon.com.au/files/mushclient/lua5.1_extras/luasocket-2.0.1.zip

    This file is 35 Kb. Its md5sum is 7ebacf720d7113a0b261c5e4e315a8c6.
  • Unzip that into the same directory as MUSHclient.exe (eg. c:\Program Files\MUSHclient if that is where you put it).
  • You should end up with three subdirectories:

    • lua (which already exists in the MUSHclient directory). This will have added to it: ltn12.lua, mime.lua and socket.lua.
    • mime - which should contain a single file: core.dll
    • socket - which should contain: core.dll, ftp.lua, http.lua, smtp.lua, tp.lua and url.lua.

  • Now, this test should work:

    
    http = require "socket.http" 
    a, b, c = http.request("http://some.web.site/")
    print (a, b, c)  -- page, return code, headers
    



What I effectively did is reorganize the files in the original .zip file that I downloaded so you didn't need to move them around as described in the earlier post.
Amended on Sat 01 Dec 2007 09:00 PM by Nick Gammon
USA #4
The link to the file goes to a nonexistent page, but I really appreciate you reorganizing the .zip; that's going to save me a lot of time and frustration.
Amended on Sat 01 Dec 2007 08:44 PM by Candido
Australia Forum Administrator #5
Sorry, I used the name of the original file, then I reorganized it and forgot to change it. The post above has been amended and should now point to the correct file.
USA #6
I unzipped the .zip and all the files were in place as you said they should be, but when I try 'http = require "socket.http"', I get the following error message:

Run-time error
World: CrownSystem
Immediate execution
C:\Program Files\MUSHclient\lua\socket.lua:13: module 'socket.core' not found:
no field package.preload['socket.core']
no file '.\socket\core.lua'
no file 'C:\Program Files\MUSHclient\lua\socket\core.lua'
no file 'C:\Program Files\MUSHclient\lua\socket\core\init.lua'
no file 'C:\Program Files\MUSHclient\socket\core.lua'
no file 'C:\Program Files\MUSHclient\socket\core\init.lua'
stack traceback:
[C]: in function 'require'
C:\Program Files\MUSHclient\lua\socket.lua:13: in main chunk
[C]: in function 'require'
C:\Program Files\MUSHclient\socket\http.lua:11: in main chunk
[C]: in function 'require'
[string "Command line"]:1: in main chunk

What does it mean?
Amended on Sat 01 Dec 2007 10:15 PM by Candido
Australia Forum Administrator #7
Go to File menu -> Global Preferences. Select the Lua tab and check "Allow DLLs to be loaded".

Then go to the Game menu -> Reload Script File. This re-initializes the script space.

If you don't do that, I get the same error message as you did. You see, the file it is looking for is a DLL.
Amended on Sat 01 Dec 2007 10:21 PM by Nick Gammon
USA #8
Everything's working perfectly now. Thank you!
Australia Forum Administrator #9
The LuaSocket files and DLLs are now included in MUSHclient version 4.60 onwards, so you should be able to just use them.
USA #10
*cheers*