Memorandum of understanding when Python is run on EC2 with Apache

A memorandum when only confirming that something can be written to + S3 that returns html called Hello World !.

Installation

$ sudo yum install httpd

Setting

/etc/httpd/conf/httpd.conf Edit

--Added ExecCGI to Options --Added index.py to DirectoryIndex --Add.py added to AddHandler cgi-script

Source code

/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

Verification

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.

Other

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

Memorandum of understanding when Python is run on EC2 with Apache
Run a batch of Python 2.7 with nohup on Amazon Linux AMI on EC2
Until you run python with apache
Run servo with Python on ESP32 (Windows)
Build python environment with pyenv on EC2 (ubuntu)
Hello World with nginx + uwsgi + python on EC2
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Run Python on Apache to view InfluxDB data
[Python Data Frame] When the value is empty, fill it with the value of another column.
Run Python with VBA
Run prepDE.py with python3
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
Run Paints Chainer on CPU with official python on win10
Run Blender with python
elasticsearch_dsl Memorandum of Understanding
Execute python3 system with PHP exec () on AWS EC2
Troublesome story when using Python3 with VScode on ubuntu
[python] When pipenv install of WSL, python on Windows side is called for some reason
Run iperf with python
Note: When Python is run on ImageJ Fiji, java.lang.IllegalArgumentException appears and its solution (mac OS)
A memorandum of stumbling on my personal HEROKU & Python (Flask)
Notes on handling large amounts of data with python + pandas
Run the intellisense of your own python library with VScode.
[python] Reverse with slices! !! (There is also a commentary on slices!)
A memo when face is detected with Python + OpenCV quickly
[GCP] A memorandum when running a Python program on Cloud Functions
1. Statistics learned with Python 2. Probability distribution [Thorough understanding of scipy.stats]
Run Openpose on Python (Windows)
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Run Tensorflow 2.x on Python 3.7
Handling of python on mac
Run Python CGI on CORESERVER
Python --Install MySQLDB on EC2
Run Label with tkinter [Python]
Error when playing with python
Full understanding of Python debugging
Run DHT22 with RasPi + Python
How is the progress? Let's get on with the boom ?? in Python
March 14th is Pi Day. The story of calculating pi with python
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
Manipulability ellipsoid of arm and mobile robot is drawn with python
Run different versions of Python (wsgi) apps on one development environment
Build Python environment on Ubuntu (when pip is not the default)
I did a lot of research on how Python is executed
A memo for when pip3 is installed with python2.7 for some reason
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
Make apache log csv with python
Memorandum on Memoization of recursive series
Preparing to run Flask on EC2
Run Rotrics DexArm with python API
[Python] A memorandum of beautiful soup4
Run mruby with Python or Blender
Memorandum on Memoization of recursive functions
Support when installing pillow on python3.9
Run Python on Schedule on AWS Lambda
Run TensorFlow Docker Image on Python3
Getting Started with Python Basics of Python
Run SwitchBot on Windows 10 with Bleak
Life game with Python! (Conway's Game of Life)
Run Aprili from Python with Orange