Login to your AWS Console.
Create and Launch a EC2 instance.
Select the EC2 instance type.
Create an SSH key pair to SSH login to the given instance.
Login to the EC2 instance and Install Python in AWS EC2 instance.
python --version
sudo apt-get install python3
python -m pip install pymongo
Python Flask is a microframework used to create simple RESTful web services.
python -m pip install Flask
Reference:
http://flask.pocoo.org/
Consider the app_controller.py as the main python file. (Initiator)
app_controller.py
#!flask/bin/python
# -*- coding: utf-8 -*-
from __future__ import division
from pymongo import MongoClient
from flask import Flask, jsonify
from flask import request
from my_first_class import MyFirstClass
from flask_api import FlaskAPI, status, exceptions
import threading
import json
app = Flask(__name__)
# Basic GET route
@app.route('/status')
def check_status():
return "OK"
# Basic POST route
@app.route('/profile', methods=['POST'])
def create_user():
print request.form.get('fname')
print request.form.get('lname')
# do processing. return data
return json.dumps(data), status.HTTP_200_OK
if __name__ == '__main__':
app.run(host='0.0.0.0', threaded=True, use_reloader=True)
# [dev localhost] app.run(threaded=True, use_reloader=True)
my_first_class.py
#!flask/bin/python
# -*- coding: utf-8 -*-
import json
from pymongo import MongoClient
class MyFirstClass:
def __init__(self):
self.client = MongoClient('mongodb://localhost:27017/')
self.icps_db = self.client['database']
def create_user(self):
documents = self.database[data_collection].find({}, no_cursor_timeout=True)
if documents is not None:
for idx, document in enumerate(documents):
raw_title = document['title'].encode('utf8')
if "USER1" in str(raw_title).lower() or "USER2" in str(raw_title).lower():
name = "MANUAL_USER"
data_record = {
"title": raw_title,
"name": name
#"words": list(word_list)
}
record_id = self.database[data_collection].insert(data_record)
print("Record created. ", record_id, " ", idx)
self.client.close()
Run the python web service as a nohup service
nohup python app_controller.py &
Stop the service
ps -ef | grep app_controller.py
kill -9 <pid>