Created
March 2, 2014 21:54
-
-
Save kisom/9314572 to your computer and use it in GitHub Desktop.
I don't even what is this has anyone ever gone so far as to even... YOLO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; sbcl --load yolo.lisp | |
;;; requires quicklisp in your .sbclrc | |
;;; or just load it in any other CL repl after loading quicklisp | |
(ql:quickload :usocket) ;; or load it some other way | |
;;;; some ideas: | |
;; echo '(defparamter *foo* 42)' | nc 127.0.0.1 4141 | |
;; echo '(+ 2 2)' | nc 127.0.0.1 4141 | |
;; used to demonstrate evaluation | |
(defvar *foo* :bar) | |
(defun socket-handler (stream) | |
"reads data from the TCP stream, and if it's a list, evaluates it." | |
(progn | |
(let ((data (read stream))) ; read that stream | |
(format t "~%~%*foo*: ~A~%" *foo*) ; show the current value of *foo* | |
(format t "READ: ~A~%" data) ; print out the data sent | |
(if (listp data) ; only eval the data if it's a proper list | |
(progn | |
;; display result of evaling the data | |
(format t "RESULT: ~A~%" (eval data)) | |
;; show the current value of *foo* | |
(format t "*foo*: ~A~%~%" *foo*)) | |
(format t "no s-expr sent, will not eval"))))) | |
;; the most basic socket server | |
(usocket:socket-server "127.0.0.1" 4141 #'socket-handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
foo = 0 | |
def new_sock(): | |
"""Return a new IPv4 TCP socket.""" | |
return socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
def server(): | |
"""Listen for new connections and attempt to eval the code coming in.""" | |
s = new_sock() | |
s.bind(("127.0.0.1", 4142)) | |
s.listen(8) | |
while True: | |
try: | |
conn, addr = s.accept() | |
sock_handler(conn, addr) | |
except KeyboardInterrupt: | |
break | |
def sock_handler(conn, addr): | |
"""Read from the socket and attempt to evaluate it.""" | |
buf = '' | |
while True: | |
tmp = conn.recv(4096) | |
if tmp == '' or tmp is None: | |
break | |
buf += tmp | |
buf = buf.strip() + '\n' | |
print "received: '%s'" % (buf, ) | |
try: | |
obj = compile(buf, "<string>", "exec") | |
print "result:", eval(obj, globals(), locals()) | |
except SyntaxError as exc: | |
print "failed:", exc | |
finally: | |
conn.close() | |
def main(): | |
"""Run that server!""" | |
server() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SERVER="127.0.0.1" | |
PORT="4141" | |
send_string () { | |
echo "$@" | nc $SERVER $PORT | |
} | |
send_file () { | |
cat $@ | nc $SERVER $PORT | |
} | |
send_string "(+ 2 2)" | |
send_string "(setf *foo* 42)" | |
send_string '(defun baz () (format t "hello world~%"))' | |
send_string "(baz)" | |
send_string ' | |
(with-open-file (stream #P"/tmp/yololisp.out" | |
:direction :output | |
:if-exists :supersede) | |
(with-standard-io-syntax | |
(format stream "hello, world~%")))' | |
send_string '(defun socket-handler (stream) | |
(progn | |
(format t "CONNECTION ~A:~%" stream) | |
(format t "NO MORE EVAL FOR YOU~%")))' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SERVER="127.0.0.1" | |
PORT="4142" | |
send_string () { | |
echo "$@" | nc $SERVER $PORT | |
} | |
send_file () { | |
cat $@ | nc $SERVER $PORT | |
} | |
send_string "2 + 2" | |
send_string "global foo; foo = 42; print 'foo:', foo" | |
send_string "print 'foo:', foo" | |
send_string "global baz | |
def baz(): | |
print 'hello, world!' | |
" | |
send_string "baz()" | |
send_string 'open("/tmp/yolopy.out", "w").write("yolo!" + chr(0x0a))' | |
send_string 'global sock_handler; | |
def sock_handler(conn, addr): | |
print "no more eval for you" | |
conn.close() | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment