简体   繁体   中英

Script to check status code of URLs using Python

I want to write script that accept multiple URLs through list or text file and append some string at the end of each URL and check https status code (200, 401 and 403)of each URL and save in separate files.

Here's my code so far:

lst = {'back.sql',
       'backup.sql',
       'accounts.sql',
       'backups.sql',
       'clients.sql',
       'customers.sql',
       'data.sql',
       'database.sql',
       'database.sqlite',
       'users.sql',
       'db.sql',
       'db.sqlite',
       'db_backup.sql',
       'dbase.sql',
       'dbdump.sql',
       'setup.sql',
       'sqldump.sql',
       'dump.sql',
       'mysql.sql',
       'sql.sql',
       'temp.sql'
       
       }
url_test = 'http://www.Holiday.com/%s/' #This can be modified to accept multiple URLs
for i in lst:
     url = url_test %i
     print(url) #This can be modified to save results for each http status code

If you want, check status code you have to request each page one by one

from requests import get

lst = {'back.sql',
           'backup.sql',
           'accounts.sql',
           'backups.sql',
           'clients.sql',
           'customers.sql',
           'data.sql',
           'database.sql',
           'database.sqlite',
           'users.sql',
           'db.sql',
           'db.sqlite',
           'db_backup.sql',
           'dbase.sql',
           'dbdump.sql',
           'setup.sql',
           'sqldump.sql',
           'dump.sql',
           'mysql.sql',
           'sql.sql',
           'temp.sql'
           
           }
url_test = ['http://www.Holiday.com/%s/'] #Create list of url
result_dict = dict()
for i in lst:
  for url_from_list in url_test:
     url = url_from_list %i
     # request and get status code from each page one by one
     result_dict[url] = get(url).status_code

result_dict will be a dictionary which will contain url as key and response code as value

Then save it to file

with open('filename.txt', 'w') as file:
  for url, status_code in result_dict.items():
    line = url+" "+str(status_code)+"\n"
    file.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM