-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceive_post_node.py
More file actions
95 lines (76 loc) · 2.9 KB
/
receive_post_node.py
File metadata and controls
95 lines (76 loc) · 2.9 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from aiohttp import web
import threading
import asyncio
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ReceivePostNode:
"""
A custom node to receive HTTP POST requests containing a string.
"""
def __init__(self):
# Start the HTTP server in a separate thread
self.loop = asyncio.new_event_loop()
self.server_thread = threading.Thread(target=self.start_server, daemon=True)
self.server_thread.start()
self.received_data = None # Store the received data
self.trigger = 0 # Dummy input to trigger re-execution
@classmethod
def INPUT_TYPES(s):
"""
Define the input fields for the node.
"""
return {
"required": {
"port": ("INT", {"default": 8082, "min": 1024, "max": 65535, "step": 1}),
"endpoint": ("STRING", {"default": "/receive"}),
"trigger": ("INT", {"default": 0}), # Dummy input to trigger re-execution
}
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("received_data",)
FUNCTION = "get_received_data"
CATEGORY = "api/http"
def start_server(self):
"""
Start an aiohttp server to listen for POST requests.
"""
app = web.Application()
app.router.add_post('/receive', self.handle_post_request)
# Run the aiohttp server
runner = web.AppRunner(app)
self.loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, '0.0.0.0', 8082) # Default port
logger.info("Starting server on 0.0.0.0:8082")
self.loop.run_until_complete(site.start())
self.loop.run_forever()
async def handle_post_request(self, request):
"""
Handle incoming POST requests and store the received string.
"""
try:
# Parse the JSON payload
data = await request.json()
self.received_data = data.get("string", "No string provided") # Extract the "string" field
# Update the trigger to force re-execution
self.trigger += 1
return web.json_response({"status": "success", "received": self.received_data})
except Exception as e:
return web.json_response({"status": "error", "message": str(e)}, status=500)
def get_received_data(self, port, endpoint, trigger):
"""
Return the last received string.
"""
return (self.received_data or "No data received",)
@classmethod
def IS_CHANGED(s, port, endpoint, trigger):
logger.info("IS_CHANGED called")
"""
Force the node to re-execute if the trigger changes.
"""
return trigger # Return the trigger value as a string
# A dictionary that contains all nodes you want to export with their names
NODE_CLASS_MAPPINGS = {
"ReceivePostNode": ReceivePostNode
}