SAE Deployment python-Let cloud automatically run Python code __python

Source: Internet
Author: User
Tags vps

Previously wrote a mock login Sina Weibo posts, however, I did not climb the microblogging data, feel a bit wasteful, so I want to write a code to send micro-blog. After writing, I feel that if I can automatically send micro-blog, but I can not start 24 hours (after 12 o'clock in the evening will be broken nets), there is no VPS (poor student dog), find a few free VPS did not, and then thought of using the Sina SAE before, I would like to try on the above.
Tried for a day or so I finally tried to come out.

The basic realization:
1, timed delivery, here is a half hour
2, off-line automatic, based on SAE, no need to open computer BlaBla
But there are also imperfect places: local I can directly simulate login, but put the code on the SAE die can not log in successfully. Finally, we can only get cookies locally and then put them on the SAE. Also do not know how long the expiration of cookies, if the failure of the need to obtain local, and then put up.
Let's talk about the deployment steps here.

Analog login, get cookies
This read this article:
http://blog.csdn.net/u011659379/article/details/48133911.

Of course, you can also directly use Httpfox to obtain cookies, and then directly. Use Httpfox to get cookies read this article:
http://blog.csdn.net/u011659379/article/details/48133121
Since I couldn't simulate a login on the SAE, I used the second method. Code to send Weibo

posturl= ' http://weibo.com/aj/mblog/add?_wv=5&__rnd= ' #+ time stamp headers={' Host ': ' D.weibo.com ', ' user-agent ': ' mozilla/5.0 (Windows NT 6.1; rv:40.0) gecko/20100101 firefox/40.0 ', ' Referer ':  
       ' Http://d.weibo.com/?from=signin '} data={' text ': ' Python test ', #这里就是要发送的内容 ' pic_id ': ', ' rank ': 0,  
       ' Rankid ': ', ' _surl ': ', ' hottopicid ': ', ' location ': ' Home ', ' module ': ' Stissue ', ' _t ': 0} #通过post的方式发送微博, cookies are the first step to obtain the cookies Requests.post (posturl,headers=headers,data=data,cookies=cookies) 
  

This is one of the simplest code to send a tweet, where the PostURL last to add a timestamp through get this address
Weibo&callback=sinassocontroller.prelogincallback&su=&rsakt=mod&client=ssologin.js (v1.4.18)
Get the Servertime. Do not know can see my analog login Micro blog article.
3. Deploy SAE
SAE Address: http://sae.sina.com.cn/
Log in using Weibo account. After logging in, create a new application


After the creation, click Apply name to manage the application.

And then "code management."

Choose SVN

Create version

And then "edit code."

Edit Config.yaml First:

name:testweibo111
version:1
cron:
-description:cron_test
  URL:/index.wsgi
  schedule: "*/30 * * * *"

The URL represents the running Index.wsgi
Schedule: "*/30 * * * *" is run every 30 minutes.
More Settings look here

and Index.wsgi the editor.
The main thing is to put the Python code in. First look at the simplest code:

Import sae# ...

def application (environ, start_response): # ...
    start_response (' OK ', [(' Content-type ', ' Text/plain ')] ... return
    [' Hello, sae! ']

Application=sae.create_wsgi_app (APP) # ...

Open the address to see:

The tail followed the # ... are indispensable. In addition to this distinction, the other is the same as using the IDE edit script. You can open the Web site at any time to see the results, and if the code goes wrong, you can see what went wrong. Like what:

After you set these up, the SAE will automatically run Python code. This is my microblog screenshot, you can see the results:

==========================================
In fact, I told the above is not very complete, because there are some things I do not understand, the expansion will be a lot of nonsense, such as the SAE Python Third-party libraries need to pack themselves up. I'm not going to talk about it here because I'm going to put all the code out and study it myself.
===========================================
If you do not understand the process above, you can directly import my program into your SAE app.

Code: HTTP://YUNPAN.CN/CMT3HZKAWSUKN Access Password d512

Remember to change Config.yaml name to your app name
Then modify the Index.wsgi to change cookies to your cookies.

The end!!
PS: In fact, this can not only be used to automatically send micro bo, digging your brain hole, you can find that this can also be used to do reptiles, free SAE program has 5G free quota, used to store data is enough ~

Related Article

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.