Skip to content

Commit

Permalink
Move server functionality into different functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tyilo committed Jan 1, 2015
1 parent 8dcc542 commit 8e2dd59
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.pyc
*.swp
venv/
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ <h4>Pyth Character Reference</h4>
}

function submit_code() {
$.get('/', {mode: 2, code: $('#code').val(), input: $('#input').val(), debug: $('#debug').is(':checked')?1:0}, function(data) {
$.get('/submit', {code: $('#code').val(), input: $('#input').val(), debug: $('#debug').is(':checked')?1:0}, function(data) {
$('#output').html(data)
})
}
Expand All @@ -147,7 +147,7 @@ <h4>Pyth Character Reference</h4>
})
}

$.get('/', {'mode': 1}, function(data) {
$.get('/web-docs.txt', function(data) {
data=data.split('\n')
for (i=0;i<data.length;i++) {
data[i]=data[i].replace(' ', '</td><td``class="arity">')
Expand Down
67 changes: 33 additions & 34 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!venv/bin/python
from flask import Flask, render_template, request, Response
import os
import time
Expand All @@ -7,40 +8,38 @@

@app.route('/')
def root():
mode = int(request.args.get('mode', 0))
if mode == 0:
time_in_secs = os.path.getmtime('safe_pyth.py')
time_in_python = time.gmtime(time_in_secs)
formatted_time = time.strftime("%d %b %Y", time_in_python)
return render_template('index.html', formatted_time=formatted_time)
elif mode == 1:
return app.send_static_file('web-docs.txt')
elif mode == 2:
resp = ''

code_message = request.args.get('code', '')
input_message = request.args.get('input', '')
debug_on = int(request.args.get('debug'), 0)

pyth_code = code_message.split("\r\n")[0]
pyth_process = \
subprocess.Popen(['/usr/bin/env',
'python3',
'safe_pyth.py',
'-cd' if debug_on else '-c',
pyth_code],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

output, errors = \
pyth_process.communicate(input=bytearray(input_message, 'utf-8'))

if code_message:
resp += 'Output: \n'
resp += '<pre class="scroll">' + output.decode() + (errors if errors else '') + '</pre>'

return Response(resp)
time_in_secs = os.path.getmtime('safe_pyth.py')
time_in_python = time.gmtime(time_in_secs)
formatted_time = time.strftime("%d %b %Y", time_in_python)
return render_template('index.html', formatted_time=formatted_time)

@app.route('/submit')
def submit():
resp = ''

code_message = request.args.get('code', '')
input_message = request.args.get('input', '')
debug_on = int(request.args.get('debug'), 0)

pyth_code = code_message.split("\r\n")[0]
pyth_process = \
subprocess.Popen(['/usr/bin/env',
'python3',
'safe_pyth.py',
'-cd' if debug_on else '-c',
pyth_code],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)

output, errors = \
pyth_process.communicate(input=bytearray(input_message, 'utf-8'))

if code_message:
resp += 'Output: \n'
resp += '<pre class="scroll">' + output.decode() + (errors if errors else '') + '</pre>'

return Response(resp)

@app.route('/<path>')
def other(path):
Expand Down

0 comments on commit 8e2dd59

Please sign in to comment.