wilson_357111317

computer_ultra.py

Nov 20th, 2025
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | Source Code | 0 0
  1. from flask import Flask, request, render_template_string
  2. import subprocess
  3. import tempfile
  4. import os
  5.  
  6. app = Flask(__name__)
  7.  
  8. HTML = """
  9. <!DOCTYPE html>
  10. <html>
  11. <head>
  12. <title>My Computer 3</title>
  13. <meta charset="UTF-8">
  14. <style>
  15. body {
  16.    background: #1e1e1e;
  17.    color: #e5e5e5;
  18.    font-family: monospace;
  19.    margin: 0;
  20.    padding: 0;
  21.    display: flex;
  22.    justify-content: center;
  23. }
  24. .container {
  25.    width: 80%;
  26.    max-width: 900px;
  27.    padding: 20px;
  28. }
  29. textarea {
  30.    width: 100%;
  31.    height: 380px;
  32.    background: #111;
  33.    color: white;
  34.    font-weight: bold;
  35.    text-shadow: 0 0 3px white;
  36.    border: 1px solid #777;
  37.    padding: 14px;
  38.    font-size: 40px;
  39.    overflow-y: scroll;
  40.    resize: none;
  41. }
  42. select, button {
  43.    padding: 14px 18px;
  44.    font-size: 45px;
  45.    margin-top: 12px;
  46. }
  47. .output-box {
  48.    width: 100%;
  49.    height: 260px;
  50.    background: white;
  51.    color: black;
  52.    font-weight: bold;
  53.    text-shadow: 0 0 3px black;
  54.    border: 1px solid #444;
  55.    padding: 14px;
  56.    margin-top: 20px;
  57.    font-size: 40px;
  58.    white-space: pre-wrap;
  59.    overflow-wrap: break-word;
  60.    word-break: break-word;
  61.    overflow-y: scroll;
  62. }
  63. h2 {
  64.    font-size: 32px;
  65. }
  66. </style>
  67. </head>
  68. <body>
  69. <div class="container">
  70.    <h2>My Computer 3</h2>
  71.    <form method="post" id="code-form">
  72.        <textarea name="code">{{ code }}</textarea><br>
  73.        <select name="mode">
  74.            <option value="python" {% if mode=='python' %}selected{% endif %}>Python</option>
  75.            <option value="sh" {% if mode=='sh' %}selected{% endif %}>Bash</option>
  76.            <option value="c" {% if mode=='c' %}selected{% endif %}>C</option>
  77.            <option value="cpp" {% if mode=='cpp' %}selected{% endif %}>C++</option>
  78.            <option value="html" {% if mode=='html' %}selected{% endif %}>HTML</option>
  79.            <option value="js" {% if mode=='js' %}selected{% endif %}>JavaScript</option>
  80.        </select>
  81.        <button type="submit" id="run-btn">Run</button>
  82.    </form>
  83.  
  84.    <div class="output-box" id="output">{{ output }}</div>
  85.  
  86. </div>
  87.  
  88. <script>
  89. const form = document.getElementById('code-form');
  90. const outputBox = document.getElementById('output');
  91.  
  92. form.addEventListener('submit', function(e) {
  93.    const mode = form.mode.value;
  94.    if(mode === 'html') {
  95.        e.preventDefault();
  96.        const htmlWindow = window.open('', '_blank');
  97.        htmlWindow.document.write(form.code.value);
  98.        htmlWindow.document.close();
  99.    }
  100. });
  101. </script>
  102. </body>
  103. </html>
  104. """
  105.  
  106. @app.route("/", methods=["GET", "POST"])
  107. def index():
  108.     code = ""
  109.     output = ""
  110.     mode = "python"
  111.  
  112.     if request.method == "POST":
  113.         code = request.form.get("code", "")
  114.         mode = request.form.get("mode", "")
  115.  
  116.         if mode == "python":
  117.             try:
  118.                 result = subprocess.check_output(["python3", "-c", code], stderr=subprocess.STDOUT).decode()
  119.                 output = result
  120.             except subprocess.CalledProcessError as e:
  121.                 output = e.output.decode()
  122.  
  123.         elif mode == "sh":
  124.             try:
  125.                 cleaned = code.replace('\r', '')
  126.                 result = subprocess.check_output(cleaned, shell=True, stderr=subprocess.STDOUT, executable="/bin/bash").decode()
  127.                 output = result
  128.             except subprocess.CalledProcessError as e:
  129.                 output = e.output.decode()
  130.  
  131.         elif mode == "c":
  132.             with tempfile.NamedTemporaryFile(delete=False, suffix=".c") as tmp:
  133.                 tmp.write(code.encode())
  134.                 tmp.flush()
  135.                 exe = tmp.name + ".out"
  136.                 comp = subprocess.run(["gcc", tmp.name, "-o", exe], capture_output=True)
  137.                 if comp.returncode != 0:
  138.                     output = comp.stderr.decode()
  139.                 else:
  140.                     run = subprocess.run([exe], capture_output=True)
  141.                     output = run.stdout.decode() + run.stderr.decode()
  142.                 os.unlink(tmp.name)
  143.                 if os.path.exists(exe):
  144.                     os.unlink(exe)
  145.  
  146.         elif mode == "cpp":
  147.             with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as tmp:
  148.                 tmp.write(code.encode())
  149.                 tmp.flush()
  150.                 exe = tmp.name + ".out"
  151.                 comp = subprocess.run(["g++", tmp.name, "-o", exe], capture_output=True)
  152.                 if comp.returncode != 0:
  153.                     output = comp.stderr.decode()
  154.                 else:
  155.                     run = subprocess.run([exe], capture_output=True)
  156.                     output = run.stdout.decode() + run.stderr.decode()
  157.                 os.unlink(tmp.name)
  158.                 if os.path.exists(exe):
  159.                     os.unlink(exe)
  160.  
  161.         elif mode == "html":
  162.             output = "HTML code opened in new tab."
  163.  
  164.         elif mode == "js":
  165.             try:
  166.                 run = subprocess.check_output(["node", "-e", code], stderr=subprocess.STDOUT).decode()
  167.                 output = run
  168.             except subprocess.CalledProcessError as e:
  169.                 output = e.output.decode()
  170.  
  171.     return render_template_string(HTML, code=code, output=output, mode=mode)
  172.  
  173. if __name__ == "__main__":
  174.     app.run(port=8000, debug=False)
  175.  
  176.  
  177.  
Tags: termux
Advertisement
Add Comment
Please, Sign In to add comment