Python

[Python] 연결 가능한 URL인지 확인하는 프로그램

Gaeun Lee 2022. 7. 15. 00:20

Python의 Requests 모듈을 활용하여 입력 받은 URL이 연결 가능한지 확인하는 코드를 작성하였다

 

참고

Requests 모듈과 OS 모듈을 사용하기 위하여 터미널에 pip install requestspip install os를 입력하여 설치한다

 

조건

  • 프로그램은 쉼표로 URL의 개수를 구별합니다. 또한 ‘http’의 유무와 공백을 체크하여 ‘http’가 없다면 추가해주고 공백은 모두 제거해 줍니다. 대문자가 포함되어 있을 경우도 생각하여 소문자로 변환시켜줍니다. 이러한 경우들을 모두 생각하여 처리해줍시다. 
  • URL이 실제로 존재하는지 존재하지 않는지 체크해야 됩니다. 
  • 사용자들은 프로그램이 모두 종료된 후 다시 시작할 수 있습니다.

 

코드

import requests
import os

while True :
    print('Welcome to IsItDown.py!')
    print('Please write a URL or URLs you want to check. (seperated by comma)')
    count = 0
    url_data = input().split(',')
    for url in url_data:
        if "." not in url:
            print(f'{url} is not a valid URL.')
            while True:
                print('Do you want to start over? y/n')
                retry = input()
                if retry == 'y' :
                    break
                elif retry == 'n' :
                    print('Ok, bye!')
                    quit()
                else : 
                    print("That's not a valid answer")
                    continue
            os.system('cls')
            continue
        else:
            count += 1
            url = url.strip()    
            if 'http://' not in url:
                url = 'http://' + url
        
            try:
                response = requests.get(url)
                print(f'{url} is up!')
            except:
                print(f'{url} is down!')

                        
            if count == len(url_data):
                while True:
                    print('Do you want to start over? y/n')
                    retry = input()
                    if retry == 'y' :
                        break
                    elif retry == 'n' :
                        print('Ok, bye!')
                        quit()
                    else : 
                        print("That's not a valid answer")
                        continue
                os.system('cls')
                continue