Configure the run on the server (a satin Python program every day)

Source: Internet
Author: User
Tags getmessage set time pscp

In the previous article, there is a program that uses PYTHON-SMTP to send a message, using a crawler to randomly crawl a piece of the Satin Web site and then sent to the designated recipient.

Running this program on the local computer is only a timely function (it is not very realistic), so now put it on the server side.

The source program needs to be slightly modified with timed statements, such as the following one-hour yellow satin, expanding the recipient list.

#coding: Utf-8import sysimport reimport urllibimport smtplibimport randomfrom email.mime.text Import mimetext# Set time on the place server import Datetimeimport timeto=[' [email protected] ', ' [email protected] ', ' [email protected ] ', [email protected] ', ' [email protected] '] host= "smtp.163.com" #smtp服务器user = "******" #用户名password = "*" # Password postfix= "163.com" #后缀def gethtml (URL): Page=urllib.urlopen (URL) html=page.read () return htmldef getmessage (HT ML): P=re.compile (R ' <div class= "Content" > (. *) </div><script type= "Text/javascript" > ') # Regular matching of the content of the Message=re.findall (p,html) #返回正则匹配的结果 return message def send_mail (to_list,sub,content): me= "jokes S ending! "      + "<" +user+ "@" +postfix+ ">" msg = mimetext (content,_subtype= ' plain ', _charset= ' utf-8 ') msg[' Subject '] = Sub msg[' from ') = Me msg[' to '] = ";". Join (to_list) Try:server = Smtplib. SMTP () server.connect (host) Server.login (User,password) sErver.sendmail (Me, To_list, msg.as_string ()) Server.close () return True except Exception, E: Print str (e) return False def task (): I=random.randint (1,500) i=str (i) web=gethtml (' Http://ish uo.cn/subject/' +i) #该网站段子的链接特点 message=getmessage (web) message2= ". Join (message) #将结果转换为字符串类型 #mess        Age2=message2.decode (' UTF8 ') message2=str (message2) print Message2 for M in range (0,5): #对收信人列表中的每个发邮件          If Send_mail (To[m], "Laugh!guys!", message2): print "suceed!" Else:print "failed!"  def timer (n): While True:task () time.sleep (n) If __name__ = = ' __main__ ': while (1): Timer (60*60) #延时一小时

  

Next take it to the server side, here we use Pscp.exe to upload

PSCP C:\Python27\smtp\smtp.py [Email protected]*****:/home/

The two are local and server paths, and then the transfer is completed after entering password.

On the server side, after SSH connection, we have a number of methods to perform this task regularly

such as the CRONTAB Directive (this program is not used, because the code is already self-delayed, if you use crontab the previous article (not changed) code can be)

Crontab-e

Edit the cron file, set the execution interval (self-setting), and execute the command, and use crontab-l to see if it was added successfully.

After

sudo service cron start

The Ps:service start command requires root permission to execute.

PS2: If there is no Python environment to build, the above program is the python2.7 version, or the lack of running libraries to install the configuration pip on the server itself.

Upon completion, the program will be able to send a message to the recipient list at a specified time interval of 24 hours a day.

Configure the run on the server (a satin Python program every day)

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.