CLIPSeg / app.py
sigyllly's picture
Update app.py
81aa0cb verified
raw
history blame
4.74 kB
from flask import Flask, request, jsonify, render_template_string
import subprocess
app = Flask(__name__)
# Route to the homepage with embedded HTML
@app.route('/')
def index():
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Administrator Terminal</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
.container {
width: 80%;
margin: 50px auto;
padding: 20px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
#terminal {
width: 100%;
height: 300px;
background-color: #000;
color: #0f0;
font-family: "Courier New", Courier, monospace;
padding: 10px;
overflow-y: scroll;
white-space: pre-wrap;
border-radius: 5px;
margin-bottom: 10px;
}
#command {
width: 100%;
padding: 10px;
font-size: 1em;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 10px 20px;
font-size: 1em;
border: none;
border-radius: 5px;
background-color: #28a745;
color: white;
cursor: pointer;
}
button:hover {
background-color: #218838;
}
</style>
</head>
<body>
<div class="container">
<h2>Administrator Terminal</h2>
<div id="terminal"></div>
<input type="text" id="command" placeholder="Enter command..." autofocus>
<button onclick="sendCommand()">Execute</button>
</div>
<script>
// Function to send command to the Flask server
function sendCommand() {
const command = document.getElementById('command').value;
if (command.trim() === '') {
alert('Please enter a command');
return;
}
fetch('/execute', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'command': command
})
})
.then(response => response.json())
.then(data => {
const terminal = document.getElementById('terminal');
if (data.stdout) {
terminal.innerHTML += '> ' + command + '\\n' + data.stdout + '\\n';
}
if (data.stderr) {
terminal.innerHTML += '> ' + command + '\\n' + data.stderr + '\\n';
}
if (data.error) {
terminal.innerHTML += '> ' + command + '\\n' + data.error + '\\n';
}
document.getElementById('command').value = '';
terminal.scrollTop = terminal.scrollHeight; // Scroll to the bottom
})
.catch(error => {
console.error('Error:', error);
});
}
// Allow pressing Enter to send the command
document.getElementById('command').addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
sendCommand();
}
});
</script>
</body>
</html>
"""
return render_template_string(html)
# Route to execute a command
@app.route('/execute', methods=['POST'])
def execute():
try:
# Get the command from the request
command = request.form['command']
# Execute the command and capture the output
result = subprocess.run(command, shell=True, capture_output=True, text=True)
# Return the output (stdout and stderr)
return jsonify({
'stdout': result.stdout,
'stderr': result.stderr
})
except Exception as e:
return jsonify({
'error': str(e)
})
if __name__ == '__main__':
app.run(debug=True)