Skip to content

Instantly share code, notes, and snippets.

@ianfun
Created July 18, 2021 08:43
Show Gist options
  • Save ianfun/fa65c9564bc2d43434943bbbd05f38d8 to your computer and use it in GitHub Desktop.
Save ianfun/fa65c9564bc2d43434943bbbd05f38d8 to your computer and use it in GitHub Desktop.
python console on browser
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet">
<meta charset="utf-8">
<title>python console</title>
</head>
<body>
<button onclick="clo();alert('shell closed');window.close()">close shell</button>
<style type="text/css">
input {
min-width: 900px;
color: green;
border-width: 0px;
}
span, input, nav {
font-family: Consolas;
font-style: oblique;
}
font{
font-size: 15px;
color: cornflowerblue;
font-family: monospace;
}
</style>
<nav id='version'>waiting for server</nav>
<pre id='shell'>
<input id="new" value="print('Silav gerokê')">
</pre>
<script type="text/javascript">
var _history = [];
var _index = 0;
var shell=document.getElementById('shell');
window.onload=function(){
var x=new XMLHttpRequest();
x.open("GET", "http://127.0.0.1:7999", true);
x.onreadystatechange=function(){
if (x.readyState == 4 && x.status == 200){
document.getElementById('version').innerText=x.response;
document.getElementById('new').focus();
}
}
x.send(null);
}
function clo(){
var x=new XMLHttpRequest();
x.open("POST", "http://127.0.0.1:7999", true);
x.send('exit(0)');
}
shell.addEventListener('keydown', function(_){
if(_.keyCode === 13){
_.preventDefault();
var old = document.getElementById('new');
var x=new XMLHttpRequest();
x.open("POST", "http://127.0.0.1:7999", true);
_history.push(old.value);
_index += 1;
x.send(old.value);
x.onreadystatechange=function(){
if (x.readyState == 4 && x.status == 200){
old.removeAttribute('id');
shell.appendChild(document.createElement('br'));
var ne = document.createElement('font');
ne.innerText=x.response;
shell.appendChild(ne);
var input = document.createElement('input');
input.setAttribute('id', 'new');
shell.appendChild(input);
input.focus();
}
}
}else if (_.keyCode==38) {
if (_index){
_index -=1 ;
document.getElementById('new').value=_history[_index];
}
}else if (_.keyCode==40) {
if (_history[_index+1]!=undefined){
_index+=1;
document.getElementById('new').value=_history[_index];
}
}
})
</script>
</body>
</html>
from http.server import *
from code import InteractiveConsole
import sys
from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
sys.ps1 = ">>> "
sys.ps2 = "... "
python = InteractiveConsole()
class handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(("Python %s on %s\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information." %(sys.version, sys.platform)).encode('utf-8'))
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
s=StringIO()
with redirect_stderr(s), redirect_stdout(s):
s.write(sys.ps2 if python.push(self.rfile.read(int(self.headers['Content-Length'])).decode('utf-8')) else sys.ps1)
self.wfile.write(s.getvalue().encode('utf-8'))
with HTTPServer(("", 7999), handler) as s:
print("shell server started")
s.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment