Hướng dẫn python script to check url status and send email alerts - tập lệnh python để kiểm tra trạng thái url và gửi cảnh báo qua email

Một tập lệnh Python đơn giản để kiểm tra URL và gửi thông báo qua email nếu có bất kỳ ngoại lệ nào. Ban đầu được tạo ra như một ứng dụng web giữ cho Ping trên Pythonanywhere để giữ cho chúng không ngủ. Cảm ơn Ogrigas cho cảm hứng.

Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều

Nhiệm vụ requests
URLS = [ = [
"http://www.google.com",,
"http://www.yahoo.com",,
]
defsend_mail (người gửi, người nhận, chủ đề, tin nhắn, mật khẩu, smtpserver): send_mail(sender, recipient, subject, message, password, smtpserver):
FromTplibImportSmtp smtplib import SMTP
fromemail.mime.textimportMIMEText email.mime.text import MIMEText
MSG = MIMETEXT (Tin nhắn) = MIMEText(message)
tin nhắn ['chủ đề'] = chủ đề['Subject'] = subject
tin nhắn ['từ'] = người gửi['From'] = sender
tin nhắn ['to'] = người nhận['To'] = recipient
Server = smtp (smtpserver) = SMTP(smtpserver)
máy chủ.ehlo ().ehlo()
server.starttls ().starttls()
server.login (người gửi, mật khẩu).login(sender, password)
server.sendmail (người gửi, [người nhận], msg.as_string ()).sendmail(sender, [recipient], msg.as_string())
server.quit ().quit()
defsend_alert (url, e, errtype): send_alert(url, e, errtype):
chủ đề = "{} cho {}". định dạng (errtype, url) = "{} for {}".format(errtype, url)
message = str (e) = str(e)
send_mail ('', '', chủ đề, tin nhắn,('', '', subject, message,
'ackspassPassword', 'smtp.gmail.com:587'), 'smtp.gmail.com:587')
# Ở trên giả định rằng bạn đang sử dụng gmail; khuyến khích nhận một ứng dụng-
# Mật khẩu cụ thể tại https://myaccount.google.com/u/0/apppasswords
forurlinurls: url in urls:
thử::
r = requests.get (url) = requests.get(url)
r.raise_for_status ().raise_for_status()
# Muốn đảm bảo rằng bất kỳ lỗi HTTP nào cũng được xử lý như một ngoại lệ
exceptrequests.exceptions.httperrorase: requests.exceptions.HTTPError as e:
send_alert (url, e, 'httperror')(url, e, 'HTTPError')
# Bắt ngoại lệ lớp cơ sở để xử lý rộng rãi tất cả các ngoại lệ
exceptrequests.exceptions.requestexceptionase: requests.exceptions.RequestException as e:
send_alert (url, e, 'ngoại lệ')(url, e, 'Exception')

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc‘get’ request and ‘smtplib’ library for sending email notification when the website is up.It means we don’t need to check every time. Our Python program will notify us via email when the site is running.
    This script simply checks whether a website is up or not. If it is up then it will send an email about this, if it is down then it will keep checking and when the site will be up, it will send an email and terminate.
    Installation: 
    Go to command prompt and write this command: 
     

    pip install requests, smtplib
    

    Dưới đây là các bước: & nbsp;

    • Đặt toàn bộ mã vào một khối thử, để xử lý ngoại lệ.
    • Gửi yêu cầu nhận đến trang web chúng tôi muốn.
    • Nếu trang web không chạy, thì chúng tôi không nhận được phản hồi do đó ném một ngoại lệ.
    • Sau đó, ngoại trừ khối, chúng tôi chỉ in trang web đó không chạy.
    • Nếu không có ngoại lệ ném, thì điều đó có nghĩa là chúng tôi đã nhận được phản hồi và trang web đang chạy.
    • Bây giờ hãy tạo phiên SMTP để đăng nhập thông qua Gmail.
    • Nhập id gmail và mật khẩu chính xác của bạn.
    • Gửi thư và thực hiện.

    Dưới đây là việc thực hiện: & NBSP;

    Python3

    import smtplib, requests, time

    from email.mime.text import MIMEText

    from email.mime.multipart import import0

    import1import2import3import4

    import5import6import7

    import8import9smtplib, requests, time0 smtplib, requests, time1

    import8smtplib, requests, time3smtplib, requests, time0 smtplib, requests, time5smtplib, requests, time6smtplib, requests, time7smtplib, requests, time8smtplib, requests, time9

    import8from1

    import8from3from4smtplib, requests, time7from6smtplib, requests, time9

    import8from9smtplib, requests, time0 email.mime.text 1email.mime.text 2smtplib, requests, time9

    import8email.mime.text 5email.mime.text 6email.mime.text 7smtplib, requests, time0 email.mime.text 9import0 import1

    import8email.mime.text 5import4email.mime.text 7smtplib, requests, time0from4

    import8email.mime.text 5MIMEText0email.mime.text 7smtplib, requests, time0MIMEText3

    import8MIMEText5smtplib, requests, time0 email.mime.text 9import0 MIMEText9

    import8from1from2from3

    import8from5smtplib, requests, time0from7

    from8import0 email.mime.text 9import0 email.mime.multipart 2

    import8email.mime.multipart 4email.mime.multipart 5from3

    import8email.mime.multipart 8from4smtplib, requests, time7MIMEText3import2

    import8import4

    import8import6import2import8smtplib, requests, time9

    import8import01

    import5import03import7

    import8import6import2import08smtplib, requests, time9

    import8import6import2import13smtplib, requests, time9

    import8import16import17smtplib, requests, time9

    import8import6import2import22smtplib, requests, time9

    import8import25


    Làm thế nào để bạn gửi một cảnh báo email trong Python?

    Sử dụng thư viện SMTPLIB tích hợp của Python để gửi email cơ bản.Gửi email với nội dung HTML và tệp đính kèm bằng gói email.Gửi nhiều email được cá nhân hóa bằng tệp CSV với dữ liệu liên hệ.Sử dụng gói Yagmail để gửi email qua tài khoản Gmail của bạn chỉ bằng một vài dòng mã.. Send emails with HTML content and attachments using the email package. Send multiple personalized emails using a CSV file with contact data. Use the Yagmail package to send email through your Gmail account using only a few lines of code.

    Làm cách nào để kiểm tra URL trong Python?

    Làm thế nào để kiểm tra một URL trong Python..
    url = "https://www.google.com".
    status_code = urllib.lời yêu cầu.Urlopen (URL).nhận được mã().
    WEBSEM_IS_UP = status_code == 200 ..
    print(website_is_up).

    Làm thế nào để bạn tự động hóa kiểm tra sức khỏe máy chủ bằng Python?

    Procedure..
    Biên dịch một kịch bản Python.# Biên dịch một tập lệnh Python có tên Health.py.....
    Tải lên và cài đặt tập lệnh Python.# Định cấu hình chức năng của DeviceA như một máy khách TFTP để tải xuống tệp python script Health.py từ máy chủ TFTP.....
    Định cấu hình một trợ lý tập lệnh.....
    Xác minh cấu hình ..