Python Программа:
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
import re
import json
import calendar, time
from datetime import datetime
from requests.exceptions import HTTPError
from requests.exceptions import Timeout
from requests.exceptions import ConnectionError
from requests.adapters import HTTPAdapter
hecurl = "https://abccompany.org:8088/services/collector/raw"
hecheaders = {
'Authorization': 'Splunk 123'
}
def ea_Info():
ea_url = "https://ea.com"
ea_headers = {
'Authorization': 'someword h78n',
'Content-Type': 'application/json',
}
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
ea_response = requests.get(ea_url, headers=ea_headers, verify=False)
Content = ea_response.text
userName = re.compile(r'.*"userName":"(.*?)"')
userName_list = (userName.findall(Content)) if userName else ""
lastActiveDate = re.compile(r'.*"lastActiveDate":"(.*?)"')
lastActiveDate_list = (lastActiveDate.findall(Content)) if lastActiveDate else ""
timestamp = datetime.strptime(lastActiveDate_list[0], "%Y-%m-%dT%H:%M:%S.000+0000")
epochtime = int(calendar.timegm(timestamp.utctimetuple()))
print("userName_list[0]",userName_list[0],type(userName_list[0]))
print("epochtime",epochtime,type(epochtime))
return userName_list[0],epochtime
RequiredData = ea_Info()
print("RequiredData :" , RequiredData , type(RequiredData))
FinalData = {}
FinalData['userName'] = RequiredData[0]
FinalData['lastActiveDate'] = RequiredData[1]
print("FinalData : " , FinalData , type(FinalData))
Adapter = HTTPAdapter(max_retries = 3)
session = requests.Session()
session.verify = False
session.mount(hecurl,Adapter)
try:
hecresponse = requests.Request('POST',hecurl, data = json.dumps(FinalData), headers=hecheaders)
prepped = session.prepare_request(hecresponse)
response = session.send(prepped)
response.raise_for_status()
if response.status_code == requests.codes.ok:
print("\nSuccessfully Logged In : " , hecurl , "\n")
Content = response.text
print("jsonoutput",response.json())
except HTTPError as http_err :
if response.status_code == 401 :
print("\nInvalid Credentials provided. \nError Message : 401 Client Error: Unauthorized. \nKindly check. \n")
else :
print("\nHTTP Error occurred : " , http_err , "\n")
except Timeout as to_err :
print("\nTimeOut Error occurred : " , to_err , "\n")
except ConnectionError as c_err :
print("\nConnectionError occurred : " , c_err , "\n")
except Exception as err :
print("\nOther Error occurred : " , err , "\n")
Вывод:
userName_list [0] AB C
время эпохи 123
RequiredData: ('AB C', 123)
FinalData: {'userName': 'AB C', 'lastActiveDate': 123}
Успешно зарегистрировано: https://abccompany.org: 8088 / services / collector / raw
jsonoutput {'text': 'Success', 'code': 0}
Проблема:
- Всегда результат объекта ответа равен 200 .
- Иногда данные отправляются успешно, иногда они не отправляются.
- Независимо от этого поведения сообщение об ошибке также не отображается.