0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

HTTP POST を file に変換する API

Posted at

次のシステムを構築する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)
#
# ---------------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?