次のシステムを構築するAPI です。
Grove IoT スターターキット for SORACOM で作るリモートカメラシステム
http_to_file.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# http_to_file.py
#
# Aug/22/2021
#
# ---------------------------------------------------------------------
import os
import sys
import json
import cgi
import fcntl
#
# ---------------------------------------------------------------------
def file_write_proc(file_name,str_out):
fp_out = open(file_name,mode='w',encoding='utf-8')
fcntl.lockf(fp_out, fcntl.LOCK_EX)
fp_out.write(str_out)
fp_out.close()
#
# ---------------------------------------------------------------------
if os.environ['REQUEST_METHOD'] == 'POST':
length, _ = cgi.parse_header(os.environ['CONTENT_LENGTH'])
data = sys.stdin.buffer.read(int(length))
#
json_str = data.decode("utf-8")
#
sys.stderr.write("*** http_to_file.py *** start ***\n")
file_name = "/var/tmp/mqtt_work/status.json"
file_write_proc(file_name,json_str)
#
data_aa = {}
str_aa = json.dumps(data_aa)
#
print('Content-Type: text/json; charset=utf-8')
print("Access-Control-Allow-Origin: *\r\n")
print(str_aa)
#
# ---------------------------------------------------------------------