-
Notifications
You must be signed in to change notification settings - Fork 40
/
SocketServerPython.py
58 lines (39 loc) · 1.31 KB
/
SocketServerPython.py
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
# SocketServerPython.py
import logging
import os
import socket
from threading import Timer
import greengrasssdk
# Creating a greengrass core sdk client
client = greengrasssdk.client('iot-data')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
streamHandler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
base_topic = os.environ['AWS_IOT_THING_NAME'] + '/socketserver'
output_topic = base_topic + '/output'
PORT = int(os.environ['PORT'])
def greengrass_socket_server_run():
global PORT
logger.info('Starting socket server')
HOST = ''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
while True:
conn, addr = s.accept()
logger.info('Connected by ' + str(addr))
data = b''
while 1:
temp = conn.recv(1024)
if not temp: break
data += temp
conn.close()
client.publish(topic=output_topic, payload=data)
def function_handler(event, context):
# Do nothing
logger.info('Doing nothing with inbound message')
# Asynchronously schedule this function to be run again in 5 seconds
Timer(5, greengrass_socket_server_run).start()