A memorandum when only confirming that something can be written to + S3 that returns html called Hello World !.
$ sudo yum install httpd
/etc/httpd/conf/httpd.conf Edit
--Added ExecCGI to Options --Added index.py to DirectoryIndex --Add.py added to AddHandler cgi-script
/var/www/html/ Create index.py in and write code
$ chmod 755 index.py
To be able to execute.
I will use boto3 so I will put it in
```$ sudo pip install boto3```
## The code I wrote
(Rewrite BucketName to your own bucket name)
#### **` index.py`**
```py
#!/usr/bin/python
# coding:utf-8
import boto3
import logging
import datetime
import cgi
import uuid
def main():
now_s = datetime.datetime.now().strftime('%Y.%m.%d-%H:%M:%S')
messge = "no-message"
field = cgi.FieldStorage() #Use if there is a message in the query parameter
if field.has_key('message'):
message = field['message'].value
# html
print "Content-Type: text/html\n"
print "Hello World!"
# S3
s3 = boto3.resource('s3')
key = 'test-folder/{0}-{1}.txt'.format(now_s, uuid.uuid4())
s3.meta.client.put_object(Bucket='BucketName', Key=key, Body=message)
# log
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger = logging.getLogger()
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.error(message)
logger.info(message)
if __name__ == "__main__":
main()
Apache Start
$ service httpd start
If you make an http request and confirm the following, it's OK.
--The response is 200, and the html "Hello World!" Is returned. --A file with a name like "date + date and time + uid.txt" is created in the test-folder of the bucket specified by S3. --Log output is possible.
When I want to get the posted body, if it is corrupted as a field or raw data, it cannot be taken from cgi.FieldStorage and I get a not indexable error. I got it with sys.stdin.read ().
How to parse the “request body” using python CGI?
Recommended Posts