Python Forum
multiple values as parameters to POST command..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiple values as parameters to POST command..
#14
Hi buran,

at last i am able to get what i need...but there is small issue i am getting formatted text and it posted to slack channel. But the problem now is its posting for one item only..actually i have two server running. However from command line its printing both instance details but onto slack its posting only one of it

#!/usr/bin/env python
#
import json
import subprocess
import sys
import requests

# Function

def awsdescribe():
    p = subprocess.Popen(["aws", "ec2", "describe-instances"], stdout=subprocess.PIPE)
    (output, err) = p.communicate()
    return output
# MAIN
desc = {}
try:
    desc = json.loads(awsdescribe())
except ValueError as ve:
    print
    "Oops! Something went wrong: {}".format(ve)
    sys.exit(1)

for reservations in desc['Reservations']:
    for instances in reservations['Instances']:
        public_ip_address = instances.get('PublicIpAddress', "-") or "-"
        private_ip_address = instances.get('PrivateIpAddress', "-") or "-"
        public_dns_name = instances.get('PublicDnsName', "-") or "-"
        private_dns_name = instances.get('PrivateDnsName', "-") or "-"

        for tags in instances['Tags']:
            key = tags['Key']
            value = tags['Value']
            if key == 'Name':
                tag_name = "{}\t".format(value)
SLACK_CHANNEL = "#xxxxxxx"
HOOK_URL = "https://hooks.slack.com/services/xxxxxx/xxxxxx/xxxxxxxxxx"
MESSAGE = {'text': tag_name + "   "    "is   " + instances['State']['Name']}
response = requests.post(
    HOOK_URL, data=json.dumps(MESSAGE),
    headers={'Content-Type': 'application/json'})

if response.status_code != 200:
    raise ValueError(
        'Request to slack returned an error %s, the response is:\n%s'
        % (response.status_code, response.text)
When I execute from command line, its showing all the instances
State Tag name
stopped abcustomvpcsubnet1
running KNginx

but when i checked slack channel its only showing..

running KNginx

how ever other instance named abcustomvpcsubnet1 not posted to slack channel

do you see placement of POST not proper ? any guidance, why its showing on screen all the records, but not on to slack...

Regards
ASP
Reply


Messages In This Thread
RE: multiple values as parameters to POST command.. - by onenessboy - Feb-23-2018, 11:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  __init__() got multiple values for argument 'schema' dawid294 4 2,281 Jan-03-2024, 09:42 AM
Last Post: buran
  How to combine multiple column values into 1? cubangt 15 2,817 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Substitue multiple substrings in one command Pavel_47 0 832 Jul-18-2022, 01:24 PM
Last Post: Pavel_47
  function accepts infinite parameters and returns a graph with those values edencthompson 0 855 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  Function - Return multiple values tester_V 10 4,439 Jun-02-2021, 05:34 AM
Last Post: tester_V
  How to input & output parameters from command line argument shantanu97 1 2,555 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Xlsxwriter: Create Multiple Sheets Based on Dataframe's Sorted Values KMV 2 3,487 Mar-09-2021, 12:24 PM
Last Post: KMV
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,872 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Assigning multiple values using tuple sivacg 2 2,265 Aug-06-2020, 10:29 PM
Last Post: perfringo
  Function parameters and values as string infobound 1 1,757 Jul-24-2020, 04:28 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020