OJ problem detection program-Python Development

Source: Internet
Author: User

Lazy people always try to make themselves more lazy, especially for computer science.

The source of the problem is simple. OJ EvaluationProgramSome problems, sometimes I don't know what's going on, it's waiting.CodeAs soon as all of them are waiting, OJ will not be able to perform normal evaluation. In this case, you need to go to the server side to solve the problem. How to solve the evaluation problem is not the focus of this discussion. Here we are more concerned about how to promptly discover the problem. How can we find the problem? In general, we need to open OJ, find the status page, and check whether the judging result is waiting. In this case, you need to open the page once in a while to check whether there are any problems, trouble! I have never thought about it. Once OJ starts waiting, you will receive a text message telling you that the OJ has a problem and you can solve it right away. How nice it is. What? Impossible? Everything is possible! The cute and powerful Python that I learned over the past two days has easily solved this problem!

What I mentioned above is a bit confusing. I don't know what OJ's children are. I 'd like to briefly describe this problem: once some text appears on the page of a website on the Internet, for example, on the homepage of the official website of 360, the word "Kingsoft" appears (joke). 360 certainly does not want the three words to appear there. However, if 360 of websites are hacked (maybe the new version is almost 0), 360 should promptly discover the three words "Kingsoft" on the page. How can this problem be found in time? Looking for a person who keeps refreshing for a day? Not realistic. The lazy will write a program and check the three words on the page at intervals (for example, three minutes, if the website fails to be accessed, you can receive a notification immediately.

Or, if you are very concerned about a website, such as a score query website, but you do not know when to start querying it, you can write a program to check it, once the website has changed or some keyword eyes appear, such as "student ID" and "name", it means you can check and get the notification immediately, we can write a program for implementation.

For the ACM child paper, you may be more interested in some competitions, such as codefroce, topcoder, avionics, and Zhejiang university competitions. You can also use this method to obtain the competition information in time to avoid missing the competition!

The program I wrote is to send an email. If you want to receive the notification immediately, you can send the email to your mobile 139 mailbox. The biggest highlight of mailbox 139 is the unlimited free SMS reminder!

This function is not complicated to say. With my current capabilities, I cannot use C, C ++, or Java or C #. I just learned a little python, it seems that python is relatively simple to implement.

With python, everything becomes so simple! More and more like python

The following is my code. There are more than 60 lines in total. I am also a beginner in Python. This is my first Python utility and the first time I came into contact with network programming. Thank you!

# -*-Coding: UTF-8 -*-
#
Search for a specified string on a website
Def Count_string (weburl, string_tobe_find ):
Import Urllib
Try :
Web = urllib. urlopen (weburl). Read ()
Except :
Return -1
String = STR (Web)
Return String. Count (string_tobe_find)

# Send email to the postmaster
Def Send_mail (sub, content ):
Import Smtplib
From Email. Mime. Text Import Mimetext
To_list = [ ' XXXXX@XXX.com ' ]
Mail_host = " Smtp.163.com " # Set Server
Mail_user = " Xxxxxxx " # User Name
Mail_pass = " Xxxxxxx " # Password
Mail_postfix = " 163. com " # Suffix of the sender

Me = " Reminder " + " < " + Mail_user +" @ " + Mail_postfix + " > "
MSG = mimetext (content, _ subtype = ' Plain ' , _ Charset = ' Gb2312 ' )
MSG [ ' Subject ' ] = Sub
MSG [ ' From ' ] = Me
MSG [ ' To ' ] = " ; " . Join (to_list)
Try :
Server = smtplib. SMTP ()
Server. Connect (mail_host)
Server. login (mail_user, mail_pass)
Server. Sendmail (Me, to_list, MSG. as_string ())
Server. Close ()
Return True
Except Exception, E:
Print STR (E)
Return False

# Check the function and call the above function
Def Check ():
Import Time
Status = count_string ( ' Http://acm.sdut.edu.cn/web/status.php ' , " Waiting " )
If Status =-1:
Print Time. ctime (Time. Time ()), " The OJ page cannot be opened. " , " Check the problem "
If Send_mail ( " The OJ page cannot be opened. " , " Check the problem! \ N \ tfrom ma6174 " ) = False:
Print ' An error occurred while sending the email. '
Return False
Time. Sleep (600)
Elif Status> 1:
Print Time. ctime (Time. Time ()), " OJ again waiting " , " Please fix it as soon as possible "
If Send_mail ( " OJ again waiting " , " Please fix it as soon as possible! \ N \ tfrom ma6174 " ) = False:
Print ' An error occurred while sending the email. '
Return False
Time. Sleep (600)
Else :
Print Time. ctime (Time. Time ()), " \ ToJ running normally! "

#Main Function
If _ Name __='_ Main __':
Print "\ ToJ run the check program --- by ma6174"
FromTimeImportSleep
WhileTrue:
Check ()

Sleep (60)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.