SAE deployment Python-lets The cloud run Python code automatically

Source: Internet
Author: User

I have previously written a post on Sina weibo, but I did not crawl the microblog data and felt a little wasted, so I wanted to write a code to Tweet. After writing to think that if you can automatically send weibo, but I could 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, and then think of the previous use of Sina sae, I would like to Try.
Tried for a day or so finally let me try out!!

The basic implementation:
1, scheduled to send, here is a half hour
2, offline automatic, based on sae, no need to open the computer BlaBla
But there are also imperfect places: local I can directly impersonate the login, but put the code on the SAE to die can not log on successfully! finally, you can only get cookies locally, and then put them on the sae! Also do not know the expiration time of the cookie, if it fails, it needs to be acquired locally, and then put up!
Let's talk about the deployment steps here!

    1. Demo login, Get Cookies
      This read this article:
      http://blog.csdn.net/u011659379/article/details/48133911.

      of course, you can use Httpfox to obtain cookies directly and then directly. Use Httpfox to get cookies read this article:
      http://blog.csdn.net/u011659379/article/details/48133121
      Since I can no longer impersonate a login on the sae, I use the second method!

    2. Code to send Weibo
Posturl='http://weibo.com/aj/mblog/add?_wv=5&__rnd='#+ time Stampheaders={'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',#This is what you want to send.       'pic_id':"',         'Rank': 0,'Rankid':"',         '_surl':"',         'Hottopicid':"',         ' location':'Home',         'Module':'stissue',         '_t': 0}#using post to send weibo, cookies are the first steps to obtain cookiesRequests.post (posturl,headers=headers,data=data,cookies=cookies)

This is the simplest code to send a microblog, where the PostURL is finally added time Stamp , is through get this address

Weibo&callback=sinassocontroller.prelogincallback&su=&rsakt=mod&client=ssologin.js (v1.4.18)
Obtained by Servertime. I do not know the article can see my analog login Micro-blog.
3. Deploy SAE
SAE Address: http://sae.sina.com.cn/
Sign in with your Weibo account. After you sign in, create a new app


Once created, click Apply name to manage the app.

and then "code management."

Choose SVN

Create version

And then the edit Code.

Edit Config.yaml First:

1cron:- description:cron_test  /index.wsgi  "*/30 ** * *" 

The URL represents the run Index.wsgi

schedule: "*/30 * * * *"The delegate runs every 30 minutes.
More settings See here

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

ImportSae#!! defApplication (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 #!!! is followed by the indispensable! In addition to this distinction, the other is the same as using the IDE editing script! You can open the URL at any time to see the results, and if the code goes wrong, you can see what went wrong. Like what:

Once these are set, the SAE will automatically run the Python code! This is my Weibo and I can see the results:

==========================================
In fact, I told the above is not very complete, because there are some things I do not understand very much, the start will be a lot of nonsense, such as the SAE Python some third-party libraries need to pack themselves up. I'm not going to talk here, because I'm going to put all the code out and do my own Research.
===========================================
If you do not understand my 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 Index.wsgi to change the 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 this can also be used to do crawlers, free SAE program has 5G free quota, used to store data is enough ~

Original Website: http://blog.csdn.net/u011659379/article/details/48314317

SAE deployment Python-lets The cloud run Python code automatically

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.