ID:28084
 
Keywords: byondhacks
This post on the forum gave me a funny idea that I'm sure will make some of you shudder. How can we embed Python code within DM?

Start off by installing Python if you haven't already got it (it comes standard with pretty much any modern OS but Windows). Next grab my byondthon.py script and run it. This script will start a fake web server on port 9001 which we'll be using to execute our Python code.

Now, fire up Dream Maker and start a new project. Paste in the following code:
proc/python(code)
        var/http[] = world.Export("http://127.0.0.1:9001?" + code)
        if(!http)
                usr << "Failed to connect."
                return

        var/F = http["CONTENT"]
        if(F)
                usr << html_encode(file2text(F))

mob/verb/test()
        python({"
import time, sys
print 'Hello from python', sys.version
print 'The time is: ' + time.asctime()
print 'Did you know that 792 to the power of 42 is ', 792**42, '?'"})

(Yes the indenting within the test verb should be like that. Otherwise Python will get confused by the extra whitespace.)

Now try running it and using the test verb.

Cool, huh? ;-)</<>
That is pretty sweet.
Cool, but not much good unless you're hosting your own server, alas.
Pretty cool trick there, but I wouldn't call it something like a "hack".
Why not? The first definition from the Jargon file:

1. n. Originally, a quick job that produces what is needed, but not well.

As Tiberath points out, this only works if you're the one hosting. Also it's pretty slow, extremely insecure and requires you to have the Python process sitting around waiting for Byond to ask it to do things. I'd call that "not well". :-)