Python evaluator via Javascript

Seo Sanghyeon unendliche at hanmail.net
Fri Sep 13 09:44:19 EDT 2002


Python FAQ Entry
4.15. Is it possible to write obfuscated one-liners in Python?
(http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.015.htp)

As we know from the FAQ, Python is a nice language for writing
obfuscated one-liners. (wink)

Now you can test your one-liners on the web -- using 'Python
evaluator via Javascript'. Since it is for one-liners, you should
type a expression. No statement allowed.

Try the following: (taken from the FAQ)

# without newline
filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,
map(lambda x,y=y:y%x,range(2,int(pow(y,0.5)+1))),1),range(2,1000)))

Mysteriously, it works. I couldn't believe it.

Copy the following 116 lines (2825 bytes) and execute. It took about
3 hours for me to write. Perhaps I have too much free time in my hands.

Tested on Win 98, Python 2.2.1, MSIE 6.0.

---- Python evaluator via Javascript ----

def prepare():
    import os
    if not os.path.exists('httpd'):
        os.mkdir('httpd')
    if not os.path.exists('httpd/cgi-bin'):
        os.mkdir('httpd/cgi-bin')
    fp = open('httpd/eval.htm', 'w')
    fp.write(dhtml)
    fp.close()
    fp = open('httpd/cgi-bin/eval.py', 'w')
    fp.write(pycgi)
    fp.close()

def server():
    import os
    os.chdir('httpd')
    from BaseHTTPServer import HTTPServer
    from CGIHTTPServer import CGIHTTPRequestHandler
    serv = HTTPServer(('', 1234), CGIHTTPRequestHandler)
    serv.serve_forever()

def main():
    import thread
    thread.start_new_thread(server, ())
    import time
    time.sleep(10)
    import webbrowser
    webbrowser.open('http://localhost:1234/eval.htm')

def idle():
    while 1:
        pass

dhtml = '''
<html>
<head>
<title>Python Evaluator via Javascript</title>
<script>
function run() {
  line = document.getElementById('expr').value;
  document.getElementById('console').value += '>>> '+line+'\\n';
  document.getElementById('expr').value = '';
  eline = escape(line).replace('+', '%2B');
  addr = 'cgi-bin/eval.py?expr='+eline;
  document.getElementById('cgi').src = addr;
}
function cls() {
  document.getElementById('console').value = '';
}
</script>
</head>
<body>
<div style="font-family: Lucida Console; position: absolute;
 left: 10px; top: 10px; width: 500px; height: 300px;">
 <textarea id="console" style="width: 500px; height: 300px;"
  disabled></textarea>
</div>
<div style="font-family: Lucida Console; position: absolute;
 left: 10px; top: 310px; width: 40px; height: 30px;">
 >>>
</div>
<div style="position: absolute;
 left: 50px; top: 310px; width: 340px; height: 30px;">
 <input type="text" id="expr" style="width: 340px;">
</div>
<div style="position: absolute;
 left: 390px; top: 310px; width: 60px; height: 30px;">
 <input type="button" style="width: 60px;" value="Eval"
  onClick="run();">
</div>
<div style="position: absolute;
 left: 450px; top: 310px; width: 60px; height: 30px;">
 <input type="button" style="width: 60px;" value="Clear"
  onClick="cls();">
</div>
<iframe id="cgi" width="0" height="0">
</body>
</html>
'''

pycgi = '''
import cgi
form = cgi.FieldStorage()

from math import *

try:
    expr = form['expr'].value
    val = str(eval(expr))
except:
    val = ''

print 'Content-Type: text/html'
print
print \'\'\'
<html>
<head>
<title>Python Evaluator via Javascript</title>
<script language="JavaScript">
function run() {
  ret = document.getElementById('val').value;
  parent.document.getElementById('console').value += ret+'\\\\n';
}
</script>
</head>
<body onLoad="run();">
<input type="hidden" id="val" value="%s">
</body>
</html>
\'\'\' % val
'''

prepare()
main()
idle()

---- Python evaluator via Javascript ----

Quiz: Why line 102 has four backslashes?

-- Seo Sanghyeon



More information about the Python-list mailing list