how to find the particular service name using If statement in Python - python

i am new to the python boto3 function in AWS. I want to know how to search for particular serviceNameArn (u'arn:aws:ecs:us-east-1:778784494011:service/RITS-selenium-node-chrome-service-Service-5ZADFVZNNCFJ) among the list of services running in the AWS ECS cluster that i have below with the key value pair: {"Browser":"Chrome"}.
here is my code:
import boto3
from flask import Flask
from flask import request
from flask import jsonify, make_response
import requests
import json
browser='chrome'
CLUSTER = 'ECS-QEAUTOMATION-HYBRID-DEV'
client = boto3.client('ecs')
list_services = client.list_services(cluster=CLUSTER)
print(list_services['serviceArns'])
for x in list_services:
if browser in x:
x.servicename
print(x.servicename)
Output:
I am still receiving the list of all services running in the cluster except one:
[u'arn:aws:ecs:us-east-1:778784494011:service/RITS-selenium-hub-service-Service-1ESSGHC030KT6', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-selenium-node-chrome-service-Service-5ZADFVZNNCFJ', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-sonarqube-service-Service-1359LNU242V25', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-jmetermaster-service-Service-1JOAYPCN8KNZI', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-jmeterslave-service-Service-1PIAW69QGP9F8', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-selenium-node-firefox-service-Service-QVDLJQ423TX7', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-dashboard-service-Service-1T8VSPQ28ZAGO', u'arn:aws:ecs:us-east-1:778784494011:service/RITS-commandcenter-service-Service-1MVRK6EPDL3MN']

Try this. It looks like you were looping through list_services instead of the arns.
browser='chrome'
CLUSTER = 'ECS-QEAUTOMATION-HYBRID-DEV'
client = boto3.client('ecs')
list_services = client.list_services(cluster=CLUSTER)
service_arns = list_services['serviceArns']
print(service_arns )
for service_arn in service_arns:
if browser in service_arn:
print(service_arn)

Related

How to put data in session using flask

this is my register() code
sesOTP = generateOTP()
session['tempOTP'] = sesOTP
and this is verifyOTP()
mOTP = session['tempOTP']
when value is initailizing in session['tempOTP'], then it is not going in verifyOTP().
I think you should do two main things. One is to import session from flask library. This can be done using the below snippet of code.
from flask import session
After that whatever you have done should be able to store the request data in session
data = request.form['data']
session['data'] = data

Issue with simple python API in flask. Trying to create a post method to add json data to a list

I am trying to build a simple flask api to post json data to a list (eventually with be redshift but this is just a simple test program).
I have attached the api code first followed by the code to send data.
I am getting internal server error issues when running the second script.
The code seems very simple though and I cannot figure out what is wrong.
from flask_restful import Api, Resource
from flask import request
app = Flask(__name__)
api = Api(app)
audit_log = []
class audit(Resource):
#def get (self):
#return {"data":"HelloWorld"}
def put (self):
new_item = request.get_json()
audit_log.append(new_item)
return new_item
api.add_resource(audit,"/")
app.run()
import requests
BASE = "HTTP://127.0.0.1:5000/"
response = requests.put(BASE, params = {'auditid' : 'xyz', 'jobname' : 'abc'})
print (response.json())
It seems that you haven't imported the Flask properly
instead of this
from flask import request
use this
from flask import Flask, request
This should work fine...

How to use Python 3 imports properly in Google Cloud Functions

I am making a simple function to check a URL status and redirect on 404. This app works fine in Flask localhost but when I move this to Google Cloud Functions, I keep getting "Error: could not handle the request". This is when my parameters on both the Cloud Function and the localhost are the exact same.
Am I doing something wrong with importing 'redirect' from Flask?
GCLOUD CODE: NOT WORKING
from flask import Flask, redirect
from flask import request
import requests
def urlincoming():
custID = request.args['custID']
token = request.args['token']
custEmail = request.args['custEmail']
storeDomain = request.args['domain']
adminEmail = request.args['adminEmail']
baseUrl = f"{storeDomain}/account/reset/{custID}/{token}"
baseUrlFailedAuth = f"{storeDomain}/account/invalid_token"
requestBaseUrl = requests.head(baseUrl)
if(requestBaseUrl.status_code == 200):
return redirect(baseUrl)
else:
return redirect(baseUrlFailedAuth)
LOCALHOST CODE: WORKING
from flask import Flask, redirect
from flask import request
import requests
app = Flask(__name__)
#app.route('/urlincoming')
def urlincoming():
custID = request.args['custID']
token = request.args['token']
custEmail = request.args['custEmail']
storeDomain = request.args['domain']
adminEmail = request.args['adminEmail']
baseUrl = f"{storeDomain}/account/reset/{custID}/{token}"
baseUrlFailedAuth = f"{storeDomain}/account/invalid_token"
requestBaseUrl = requests.head(baseUrl)
if(requestBaseUrl.status_code == 200):
return redirect(baseUrl)
else:
return redirect(baseUrlFailedAuth)
All Google Cloud Functions need to have one of the following two signatures:
HTTP Functions:
function_name(request):
...
Background functions:
function_name(data, context):
...
Depending on the type of function you're creating, you either need to add the request or data, context arguments.
from flask import redirect
import requests
def urlincoming(request):
I was able to fix things by adding the request as a argument but I'm not sure why it worked :/

Flask API not receiving requests all of a sudden

I am trying to create a REST API in Flask. The thing is it runs perfectly for a few days and then all of a sudden it STOPS receiving requests altogether. Forget about not responding to requests; it just doesn't receive any requests at the first place. This is my script:
from flask import Flask, jsonify
from flask_restful import Resource, Api
from flask_restful import reqparse
from sqlalchemy import create_engine
from flask.ext.httpauth import HTTPBasicAuth
from flask.ext.cors import CORS
conn_string = "mssql+pyodbc://x"
e = create_engine(conn_string)
auth = HTTPBasicAuth()
#auth.get_password
def get_password(username):
if username == 'x':
return 'x'
return None
app = Flask(__name__)
cors = CORS(app)
api = Api(app)
class Report(Resource):
decorators = [auth.login_required]
def get(self):
parser = reqparse.RequestParser()
parser.add_argument('start', type = str)
parser.add_argument('end', type = str)
args = parser.parse_args()
conn = e.connect()
stat = """
select a, b from report where c < ? and d > ?
"""
query = conn.execute(stat, [args['start'], args['end']])
json_dict = []
for i in query.cursor.fetchall():
res = {'aa': i[0], 'bb':i[1]}
json_dict.append(res)
conn.close()
return jsonify(results=json_dict)
api.add_resource(Report, '/report')
if __name__ == '__main__':
app.run(host='0.0.0.0')
I've tried to debug the issue and following are my observations:
1) Flask API is running on port 5000 and when I psping the VM on port 5000 I'm able to connect which means the process is actually running properly on the VM.
2) On checking my logs, the GET requests are not even being received by the API. If there was some db error then I'd have gotten a 500 error message but the requests are not even going to the API at the first place.
3) If I call the API locally then still the issue persists.
4) If I do a netstat for port 5000 (where my flask API is running on) I'm getting the following:
For some reason I think its not closing socket connections. I'm getting lots of "CLOSE_WAIT". Is this what is causing the problem? How can I fix this in my code?
Usually, when you get lots CLOSE_WAIT status, it means there are socket connections unclosed. And It seems that you have found the answer at Flask / Werkzeug - sockets stuck in CLOSE_WAIT , which leverages Tornado http://flask.pocoo.org/docs/0.10/deploying/wsgi-standalone/#tornado to build a non-blocking web server.

Heroku MongoHQ add-on and PyMongo -- OperationFailure: database error: unauthorized

I'm having trouble with the MongoHQ Heroku addon. Locally my app works and the os variable is present and well-formed on Heroku. However, when I attempt to access the db it throws an error: OperationFailure: database error: unauthorized db:my_database ns:my_database.cars lock type:0 client:128.62.187.133. If I try to hard-code the connection string from MongoHQ and run locally, I get the same error.
My app is below:
import os
import datetime
from flask import Flask
from flask import g
from flask import jsonify
from flask import json
from flask import request
from flask import url_for
from flask import redirect
from flask import render_template
from flask import make_response
import pymongo
from pymongo import Connection
from bson import BSON
from bson import json_util
app = Flask(__name__)
def mongo_conn():
# Format: MONGOHQ_URL: mongodb://<user>:<pass>#<base_url>:<port>/<url_path>
if os.environ.get('MONGOHQ_URL'):
return Connection(os.environ['MONGOHQ_URL'])
else:
return Connection()
#app.route('/', methods=['GET', 'POST'])
def hello():
# Get your DB
connection = mongo_conn()
db = connection.my_database
# Create an object
car = {"brand": "Ford",
"model": "Mustang",
"date": datetime.datetime.utcnow()}
# Get your collection
cars = db.cars # crashes
# Insert it
cars.insert(car)
...
Edit: MongoHQ support helped me. Problem was that I was calling my database my_database instead of the actual DB name given to me by the MongoHQ addon. E.g., db = connection.app52314314. That change fixed it.
You likely need to run the authenticate command against the DB directly after you connect.
Try something like this:
db.authenticate([USER], [PASSWORD])
If that doesn't work, feel free to email support#mongohq.com and we can help you out with your specific DB.
You don't need to do all that. You can simply:
from pymongo import MongoClient
client = MongoClient(os.environ['MONGOHQ_URL'])
mongo_db = client.get_default_database()
It will automatically authenticate you, and connect to the provisioned database, the <url_path> part of your connection url.

Categories