import json
import sys
import requests


def py_version():
    if sys.version_info < (3, 0, 0):
        print(sys.version)
        print('You must use Python 3.x to run this application.')
        sys.exit(1)


def get_version(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = json.loads(r.text)['version']['number']

    return es_version


def test_con(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = get_version(endpoint)

    if r.status_code == 200:
        print('ESVersion: {}'.format(es_version))
        print('Connection: OK')
        print('Status: {}\n'.format(r.status_code))
    else:
        print(json.loads(msg)['Message'])
        print('Status: {}'.format(status_code))
        sys.exit(1)

import json
import sys
import requests


def py_version():
    if sys.version_info < (3, 0, 0):
        print(sys.version)
        print('You must use Python 3.x to run this application.')
        sys.exit(1)


def get_version(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = json.loads(r.text)['version']['number']

    return es_version


def test_con(endpoint):
    r = requests.get('https://{}'.format(endpoint))

    try:
        es_version = get_version(endpoint)
    except KeyError:
        print('Status: {}'.format(r.status_code))
        sys.exit(1)
    else:
        if r.status_code == 200:
            print('ESVersion: {}'.format(es_version))
            print('Connection: OK')
            print('Status: {}\n'.format(r.status_code))

Fix KeyError: 'version' due to 403 Forbidden error
Fix KeyError: 'version' due to 403 Forbidden error

  import json
  import sys
  import requests
  
  
  def py_version():
      if sys.version_info < (3, 0, 0):
          print(sys.version)
          print('You must use Python 3.x to run this application.')
          sys.exit(1)
  
  
  def get_version(endpoint):
      r = requests.get('https://{}'.format(endpoint))
      es_version = json.loads(r.text)['version']['number']
  
      return es_version
  
  
  def test_con(endpoint):
      r = requests.get('https://{}'.format(endpoint))
-     es_version = get_version(endpoint)
  
-     if r.status_code == 200:
-         print('ESVersion: {}'.format(es_version))
-         print('Connection: OK')
+     try:
+         es_version = get_version(endpoint)
+     except KeyError:
-         print('Status: {}\n'.format(r.status_code))
+         print('Status: {}'.format(r.status_code))
+         sys.exit(1)
      else:
-         print(json.loads(msg)['Message'])
+         if r.status_code == 200:
+             print('ESVersion: {}'.format(es_version))
+             print('Connection: OK')
-         print('Status: {}'.format(status_code))
+             print('Status: {}\n'.format(r.status_code))
-         sys.exit(1)
  
Fix KeyError: 'version' due to 403 Forbidden error
## Code Before:
import json
import sys
import requests


def py_version():
    if sys.version_info < (3, 0, 0):
        print(sys.version)
        print('You must use Python 3.x to run this application.')
        sys.exit(1)


def get_version(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = json.loads(r.text)['version']['number']

    return es_version


def test_con(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = get_version(endpoint)

    if r.status_code == 200:
        print('ESVersion: {}'.format(es_version))
        print('Connection: OK')
        print('Status: {}\n'.format(r.status_code))
    else:
        print(json.loads(msg)['Message'])
        print('Status: {}'.format(status_code))
        sys.exit(1)

## Instruction:
Fix KeyError: 'version' due to 403 Forbidden error
## Code After:
import json
import sys
import requests


def py_version():
    if sys.version_info < (3, 0, 0):
        print(sys.version)
        print('You must use Python 3.x to run this application.')
        sys.exit(1)


def get_version(endpoint):
    r = requests.get('https://{}'.format(endpoint))
    es_version = json.loads(r.text)['version']['number']

    return es_version


def test_con(endpoint):
    r = requests.get('https://{}'.format(endpoint))

    try:
        es_version = get_version(endpoint)
    except KeyError:
        print('Status: {}'.format(r.status_code))
        sys.exit(1)
    else:
        if r.status_code == 200:
            print('ESVersion: {}'.format(es_version))
            print('Connection: OK')
            print('Status: {}\n'.format(r.status_code))
