GridTalk.de
Server_Control_Panel in Python3 und html - Druckversion

+- GridTalk.de (https://www.gridtalk.de)
+-- Forum: Werkstatt (https://www.gridtalk.de/forumdisplay.php?fid=4)
+--- Forum: Technik (https://www.gridtalk.de/forumdisplay.php?fid=25)
+--- Thema: Server_Control_Panel in Python3 und html (/showthread.php?tid=4807)



Server_Control_Panel in Python3 und html - Manfred Aabye - 10.02.2024

Dies ist ein Experiment, welches ein Webinterface mit Python 3 über HTML bereitstellt.

   

osmwebtool.py siehe ZIP Anhang
Code:
from flask import Flask, request, jsonify
from flask_httpauth import HTTPBasicAuth
import subprocess
import os
import time

# Installieren Sie Flask, Flask-HTTPAuth und subprocess wenn Sie es noch nicht installiert haben:
# pip install Flask
# pip install Flask-HTTPAuth
# pip install subprocess

# Öffnen Sie Ihren Webbrowser und navigieren Sie zur Adresse http://localhost:5000/, um Ihre Python-basierte Benutzeroberfläche anzuzeigen.

app = Flask(__name__)
auth = HTTPBasicAuth()

# Benutzername und Passwort
users = {
    "username": "password"
}

@auth.verify_password
def verify_password(username, password):
    if username in users and users[username] == password:
        return username

STARTVERZEICHNIS = '/opt'  # Geben Sie die Startverzeichnisse an
ROBUSTVERZEICHNIS = 'robust'
MONEYVERZEICHNIS = 'robust'
DOTNETMODUS = "yes" # Alten oder neuen OpenSimulator.

@app.route('/osstart', methods=['POST'])
def osstart():
    OSSTARTSCREEN = request.json.get('osstartscreen')
    print(f'OpenSimulator {OSSTARTSCREEN} Starten')

    if not is_screen_running(OSSTARTSCREEN):
        if os.path.isdir(f'{STARTVERZEICHNIS}/{OSSTARTSCREEN}'):
            os.chdir(f'{STARTVERZEICHNIS}/{OSSTARTSCREEN}/bin')
            subprocess.run(['screen', '-X', '-S', OSSTARTSCREEN, 'kill'])

            if DOTNETMODUS == "yes":
                subprocess.run(['screen', '-fa', '-S', OSSTARTSCREEN, '-d', '-U', '-m', 'dotnet', 'OpenSim.dll'])
            elif DOTNETMODUS == "no":
                subprocess.run(['screen', '-fa', '-S', OSSTARTSCREEN, '-d', '-U', '-m', 'mono', 'OpenSim.exe'])
            
            time.sleep(20)
        else:
            print(f'OpenSimulator {OSSTARTSCREEN} nicht vorhanden')
            return jsonify({'message': 'OpenSimulator nicht vorhanden'}), 404
    else:
        print(f'OpenSimulator {OSSTARTSCREEN} laeuft bereits')
        return jsonify({'message': 'OpenSimulator laeuft bereits'}), 400

    return jsonify({'message': 'OpenSimulator gestartet'}), 200

@app.route('/osstop', methods=['POST'])
def osstop():
    OSSTOPSCREEN = request.json.get('osstopscreen')

    if is_screen_running(OSSTOPSCREEN):
        print(f'OpenSimulator {OSSTOPSCREEN} Beenden')
        subprocess.run(['screen', '-S', OSSTOPSCREEN, '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        time.sleep(60)
        subprocess.run(['screen', '-X', '-S', OSSTOPSCREEN, 'kill'])
        return jsonify({'message': 'OpenSimulator beendet'}), 200
    else:
        print(f'OpenSimulator {OSSTOPSCREEN} nicht vorhanden')
        return jsonify({'message': 'OpenSimulator nicht vorhanden'}), 404

@app.route('/rostart', methods=['POST'])
def rostart():
    print("Robust wird gestartet...")
    os.chdir(f'{STARTVERZEICHNIS}/{ROBUSTVERZEICHNIS}/bin')

    if DOTNETMODUS == "yes":
        subprocess.run(['screen', '-fa', '-S', 'RO', '-d', '-U', '-m', 'dotnet', 'Robust.dll'])
    elif DOTNETMODUS == "no":
        subprocess.run(['screen', '-fa', '-S', 'RO', '-d', '-U', '-m', 'mono', 'Robust.exe'])

    time.sleep(60)
    print("Robust wurde gestartet")
    return jsonify({'message': 'Robust gestartet'}), 200

@app.route('/rostop', methods=['POST'])
def rostop():
    if is_screen_running('RO'):
        subprocess.run(['screen', '-S', 'RO', '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        print("Robust Beenden")
        time.sleep(60)
        return jsonify({'message': 'Robust beendet'}), 200
    else:
        print("Robust nicht vorhanden")
        return jsonify({'message': 'Robust nicht vorhanden'}), 404

@app.route('/mostart', methods=['POST'])
def mostart():
    print("Money wird gestartet...")
    os.chdir(f'{STARTVERZEICHNIS}/{MONEYVERZEICHNIS}/bin')

    if DOTNETMODUS == "yes":
        subprocess.run(['screen', '-fa', '-S', 'MO', '-d', '-U', '-m', 'dotnet', 'MoneyServer.dll'])
    elif DOTNETMODUS == "no":
        subprocess.run(['screen', '-fa', '-S', 'MO', '-d', '-U', '-m', 'mono', 'MoneyServer.exe'])

    time.sleep(60)
    print("Money wurde gestartet")
    return jsonify({'message': 'Money gestartet'}), 200

@app.route('/mostop', methods=['POST'])
def mostop():
    if is_screen_running('MO'):
        subprocess.run(['screen', '-S', 'MO', '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        print("Money Beenden")
        time.sleep(60)
        return jsonify({'message': 'Money beendet'}), 200
    else:
        print("Money nicht vorhanden")
        return jsonify({'message': 'Money nicht vorhanden'}), 404

@app.route('/osrestart', methods=['POST'])
def osrestart():
    osstop()
    mostop()
    rostop()    
    rostart()
    mostart()
    osstart()
    return jsonify({'message': 'OpenSimulator Neustart erfolgreich'}), 200

def is_screen_running(screen_name):
    result = subprocess.run(['screen', '-list'], stdout=subprocess.PIPE)
    return screen_name.encode() in result.stdout

@app.route('/')
@auth.login_required
def index():
    return '''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Server Control Panel</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
            }
            h1 {
                text-align: center;
            }
            .button-container {
                display: flex;
                justify-content: center;
                margin-top: 20px;
            }
            button {
                padding: 10px 20px;
                margin: 0 10px;
                font-size: 16px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <h1>Server Control Panel</h1>
        <div class="button-container">
            <button id="osstart">Start OpenSimulator</button>
            <button id="osstop">Stop OpenSimulator</button>
            <button id="rostart">Start Robust</button>
            <button id="rostop">Stop Robust</button>
            <button id="mostart">Start Money</button>
            <button id="mostop">Stop Money</button>
            <button id="osrestart">Restart OpenSimulator</button>
        </div>

        <script>
            document.getElementById('osstart').addEventListener('click', function() {
                // Code zum Starten von OpenSimulator
            });

            document.getElementById('osstop').addEventListener('click', function() {
                // Code zum Stoppen von OpenSimulator
            });

            document.getElementById('rostart').addEventListener('click', function() {
                // Code zum Starten von Robust
            });

            document.getElementById('rostop').addEventListener('click', function() {
                // Code zum Stoppen von Robust
            });

            document.getElementById('mostart').addEventListener('click', function() {
                // Code zum Starten von Money
            });

            document.getElementById('mostop').addEventListener('click', function() {
                // Code zum Stoppen von Money
            });

            document.getElementById('osrestart').addEventListener('click', function() {
                // Code zum Neustarten von OpenSimulator
            });
        </script>
    </body>
    </html>
    '''

if __name__ == '__main__':
    app.run(debug=True)



RE: Server_Control_Panel in Python3 und html - Bogus Curry - 10.02.2024

(10.02.2024, 07:40)Manfred Aabye schrieb: Dies ist ein Experiment, welches ein Webinterface mit Python 3 über HTML bereitstellt.



osmwebtool.py siehe ZIP Anhang
Code:
from flask import Flask, request, jsonify
from flask_httpauth import HTTPBasicAuth
import subprocess
import os
import time

# Installieren Sie Flask, Flask-HTTPAuth und subprocess wenn Sie es noch nicht installiert haben:
# pip install Flask
# pip install Flask-HTTPAuth
# pip install subprocess

# Öffnen Sie Ihren Webbrowser und navigieren Sie zur Adresse http://localhost:5000/, um Ihre Python-basierte Benutzeroberfläche anzuzeigen.

app = Flask(__name__)
auth = HTTPBasicAuth()

# Benutzername und Passwort
users = {
    "username": "password"
}

@auth.verify_password
def verify_password(username, password):
    if username in users and users[username] == password:
        return username

STARTVERZEICHNIS = '/opt'  # Geben Sie die Startverzeichnisse an
ROBUSTVERZEICHNIS = 'robust'
MONEYVERZEICHNIS = 'robust'
DOTNETMODUS = "yes" # Alten oder neuen OpenSimulator.

@app.route('/osstart', methods=['POST'])
def osstart():
    OSSTARTSCREEN = request.json.get('osstartscreen')
    print(f'OpenSimulator {OSSTARTSCREEN} Starten')

    if not is_screen_running(OSSTARTSCREEN):
        if os.path.isdir(f'{STARTVERZEICHNIS}/{OSSTARTSCREEN}'):
            os.chdir(f'{STARTVERZEICHNIS}/{OSSTARTSCREEN}/bin')
            subprocess.run(['screen', '-X', '-S', OSSTARTSCREEN, 'kill'])

            if DOTNETMODUS == "yes":
                subprocess.run(['screen', '-fa', '-S', OSSTARTSCREEN, '-d', '-U', '-m', 'dotnet', 'OpenSim.dll'])
            elif DOTNETMODUS == "no":
                subprocess.run(['screen', '-fa', '-S', OSSTARTSCREEN, '-d', '-U', '-m', 'mono', 'OpenSim.exe'])
            
            time.sleep(20)
        else:
            print(f'OpenSimulator {OSSTARTSCREEN} nicht vorhanden')
            return jsonify({'message': 'OpenSimulator nicht vorhanden'}), 404
    else:
        print(f'OpenSimulator {OSSTARTSCREEN} laeuft bereits')
        return jsonify({'message': 'OpenSimulator laeuft bereits'}), 400

    return jsonify({'message': 'OpenSimulator gestartet'}), 200

@app.route('/osstop', methods=['POST'])
def osstop():
    OSSTOPSCREEN = request.json.get('osstopscreen')

    if is_screen_running(OSSTOPSCREEN):
        print(f'OpenSimulator {OSSTOPSCREEN} Beenden')
        subprocess.run(['screen', '-S', OSSTOPSCREEN, '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        time.sleep(60)
        subprocess.run(['screen', '-X', '-S', OSSTOPSCREEN, 'kill'])
        return jsonify({'message': 'OpenSimulator beendet'}), 200
    else:
        print(f'OpenSimulator {OSSTOPSCREEN} nicht vorhanden')
        return jsonify({'message': 'OpenSimulator nicht vorhanden'}), 404

@app.route('/rostart', methods=['POST'])
def rostart():
    print("Robust wird gestartet...")
    os.chdir(f'{STARTVERZEICHNIS}/{ROBUSTVERZEICHNIS}/bin')

    if DOTNETMODUS == "yes":
        subprocess.run(['screen', '-fa', '-S', 'RO', '-d', '-U', '-m', 'dotnet', 'Robust.dll'])
    elif DOTNETMODUS == "no":
        subprocess.run(['screen', '-fa', '-S', 'RO', '-d', '-U', '-m', 'mono', 'Robust.exe'])

    time.sleep(60)
    print("Robust wurde gestartet")
    return jsonify({'message': 'Robust gestartet'}), 200

@app.route('/rostop', methods=['POST'])
def rostop():
    if is_screen_running('RO'):
        subprocess.run(['screen', '-S', 'RO', '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        print("Robust Beenden")
        time.sleep(60)
        return jsonify({'message': 'Robust beendet'}), 200
    else:
        print("Robust nicht vorhanden")
        return jsonify({'message': 'Robust nicht vorhanden'}), 404

@app.route('/mostart', methods=['POST'])
def mostart():
    print("Money wird gestartet...")
    os.chdir(f'{STARTVERZEICHNIS}/{MONEYVERZEICHNIS}/bin')

    if DOTNETMODUS == "yes":
        subprocess.run(['screen', '-fa', '-S', 'MO', '-d', '-U', '-m', 'dotnet', 'MoneyServer.dll'])
    elif DOTNETMODUS == "no":
        subprocess.run(['screen', '-fa', '-S', 'MO', '-d', '-U', '-m', 'mono', 'MoneyServer.exe'])

    time.sleep(60)
    print("Money wurde gestartet")
    return jsonify({'message': 'Money gestartet'}), 200

@app.route('/mostop', methods=['POST'])
def mostop():
    if is_screen_running('MO'):
        subprocess.run(['screen', '-S', 'MO', '-p', '0', '-X', 'eval', "stuff 'shutdown^M'"])
        print("Money Beenden")
        time.sleep(60)
        return jsonify({'message': 'Money beendet'}), 200
    else:
        print("Money nicht vorhanden")
        return jsonify({'message': 'Money nicht vorhanden'}), 404

@app.route('/osrestart', methods=['POST'])
def osrestart():
    osstop()
    mostop()
    rostop()    
    rostart()
    mostart()
    osstart()
    return jsonify({'message': 'OpenSimulator Neustart erfolgreich'}), 200

def is_screen_running(screen_name):
    result = subprocess.run(['screen', '-list'], stdout=subprocess.PIPE)
    return screen_name.encode() in result.stdout

@app.route('/')
@auth.login_required
def index():
    return '''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Server Control Panel</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
            }
            h1 {
                text-align: center;
            }
            .button-container {
                display: flex;
                justify-content: center;
                margin-top: 20px;
            }
            button {
                padding: 10px 20px;
                margin: 0 10px;
                font-size: 16px;
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <h1>Server Control Panel</h1>
        <div class="button-container">
            <button id="osstart">Start OpenSimulator</button>
            <button id="osstop">Stop OpenSimulator</button>
            <button id="rostart">Start Robust</button>
            <button id="rostop">Stop Robust</button>
            <button id="mostart">Start Money</button>
            <button id="mostop">Stop Money</button>
            <button id="osrestart">Restart OpenSimulator</button>
        </div>

        <script>
            document.getElementById('osstart').addEventListener('click', function() {
                // Code zum Starten von OpenSimulator
            });

            document.getElementById('osstop').addEventListener('click', function() {
                // Code zum Stoppen von OpenSimulator
            });

            document.getElementById('rostart').addEventListener('click', function() {
                // Code zum Starten von Robust
            });

            document.getElementById('rostop').addEventListener('click', function() {
                // Code zum Stoppen von Robust
            });

            document.getElementById('mostart').addEventListener('click', function() {
                // Code zum Starten von Money
            });

            document.getElementById('mostop').addEventListener('click', function() {
                // Code zum Stoppen von Money
            });

            document.getElementById('osrestart').addEventListener('click', function() {
                // Code zum Neustarten von OpenSimulator
            });
        </script>
    </body>
    </html>
    '''

if __name__ == '__main__':
    app.run(debug=True)

Wow das sieht super aus, respekt ;D