STUDY

python -

02:00AM 2022. 8. 30. 21:25
from requests import get
#4.9 recap - update http statuscode

websites = (
  "google.com",
  "https://httpstat.us/300",
  "https://httpstat.us/400",
  "https://httpstat.us/500",
  "https://naver.com"
)

http_state_code = {
  200: {
    'message':'200 OK',
    'description':'The request succeeded. The result meaning of "success" depends on the HTTP method'
  },
  300: {
    'message':'300 Multiple Choices',
    'description':'The request has more than one possible response. The user agent or user should choose one of them. (There is no standardized way of choosing one of the responses, but HTML links to the possibilities are recommended so the user can pick.)'
  },
  400: {
    'message':'400 Bad Request',
    'description':'The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).'
  },
  500: {
    'message':"500 Internal Server Error",
    'description':'The server has encountered a situation it does not know how to handle.'
  },
}
results = {}

def fn_combine(url,code):
  http_detail = {
    'url':url,
    'code': code,
    'message': http_state_code[code]['message'],
    'description': http_state_code[code]['description']
  }
  
  results[website] = http_detail
  return results
  
def fn_status_code_check_and_result_combine(url):
  response = get(url)
  status_code = response.status_code
  
  if status_code == 200:
    fn_combine(url, status_code)
  elif status_code == 300:
    fn_combine(url, status_code)
  elif status_code == 400:
    fn_combine(url, status_code)
  elif status_code == 500:
    fn_combine(url, status_code)
    
for website in websites:
  if not website.startswith("https://"):
    website = f"https://{website}"
  fn_status_code_check_and_result_combine(website)

print(results)
#print(results.get("https://google.com"))

'STUDY' 카테고리의 다른 글

python  (0) 2022.09.01
python loop  (0) 2022.08.30
python data structures  (0) 2022.08.30