File size: 1,313 Bytes
e88ff52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import websockets
import json

async def test_runner():
    uri = "ws://localhost:8001/ws/run"
    async with websockets.connect(uri) as websocket:
        code = """
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Backend Python Runner!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
"""
        payload = {
            "code": code,
            "requirements": "flask"
        }
        
        print("Sending payload...")
        await websocket.send(json.dumps(payload))
        
        print("Waiting for logs...")
        try:
            while True:
                response = await websocket.recv()
                data = json.loads(response)
                
                if data["type"] == "log":
                    print(f"LOG: {data['content']}", end="")
                    # Check if Flask started
                    if "Running on http" in data["content"]:
                        print("\n[SUCCESS] Flask is running!")
                        break
                elif data["type"] == "error":
                    print(f"ERROR: {data['message']}")
                    break
        except Exception as e:
            print(f"Exception: {e}")

if __name__ == "__main__":
    asyncio.run(test_runner())