Sae-Python tutorial (1) Python development on SAE

Source: Internet
Author: User

Note:

(1) I am a beginner in Python and web development. The content of this article is only for beginners to exchange experiences ...... In fact, I want to say that you are welcome to speak out.

(2) I think Sae is not a very useful environment for beginners of Python. It is recommended that you install nginx and uwsgi to store web services locally for beginners to learn.

1.1 apply to try out the python service of SAE

For example, if the SAE account does not see python in the development language options on the create application interface, you need to apply here (http://www.douban.com/group/topic/24041553. I can create a Python application about one day after applying.

In addition, you can join the Sae-Python email list and the SAE Python team of Douban to communicate with developers.

1.2 create the first Python application

If the application is successful in the previous step, you can see that the selection of the development language interface is more than the python option. The rest is the same as the creation of an sae PHP application.

Fill in the relevant information and open the application

On the Application homepage, click "code management" and create a version. The system automatically generates a code framework: config. yaml and index. wsgi.

At this time, click the link of this application to see the page showing"Welcome to Sae!"Indicates the creation is successful.

1.3 write your own Hello World

Now, you can use the SAE Editor (which is actually a very bad thing) to edit code online. Currently, there are only two files, config. yaml and index. wsgi.

For the moment, we can look at index. wsgi without worrying about config. yaml.

The file suffixed with. wsgi is actually a python file. It first introduces the SAE module, and then returns an HTTP format content in the app function, that is, what you seeWelcome to Sae!".

LastSae.Create_wsgi_appConverts an app to an application applicable to the SAE environment. (For details, refer to the description of "Sae Python environment" in the official document)

Therefore, the final content we see is determined by the return value of this app function.

Let's add a piece of code before the app definition, and then replace the return part of the app function. Now the code looks like this

import saedef hello():    return 'hello my python'def app(environ, start_response):    status = '200 OK'    response_headers = [('Content-type', 'text/html; charset=utf-8')]    start_response(status, response_headers)    return hello()   application = sae.create_wsgi_app(app)

Save and access the application. If you see "Hello my python", the original content is successfully replaced.

Haha, will the first lesson of this tutorial be finished here?

1.4 why write Hello world like this?

In fact, you can directly replace the return part of the app function with the subsequent string and see the effect. So is this an additional step?

First, let's take a look at the frameworks provided by SAE that can be used. The official document "Quick Start" lists all supported frameworks and their corresponding Quick Start modes. If you have learned how to use frameworks such as Django, Web. py, and Tornado, it should be fine for you.

-- The problem is that you do not want to use the framework?

If you have the patience to continue reading the "Study Notes" that I can't stand, please change the code to the following:

import saedef hello():    return 'hello my python'    def hello2():    print 'hello 2'    returndef app(environ, start_response):    status = '200 OK'    response_headers = [('Content-type', 'text/html; charset=utf-8')]    start_response(status, response_headers)    hello2()    return hello()   application = sae.create_wsgi_app(app)

I added a function named hello2 and executed a print statement. Then, call hello2 in the app function.

Run now.

As a result, the output of the print statement is certainly not printed on the browser interface. However, this hello2 function is also executed. Therefore, in SAE, we cannot see the print output like running python on the command line, but this does not prevent us from letting Python do what it should do. It also supports the introduction of various modules, do a variety of powerful things-we can only call it through one URL. So when you access the application URL, you are actually executing the app function.

If you define multiple functions to be executed, you can put all of them into the app function for calling.

1.5 final

After talking a lot about it, I suggest you select a Framework for Development on SAES. In this case, the question above is "why do you write such a hello world ?" -- When your functions and objects start to increase, but there is no framework to manage them, you need to create (or several) py files to store the code, then write some code to manage the calls of functions and objects. At this time, you will find that using a Web framework is still helpful in this case.

Below are several links for your reference:

(1) get started with Python + Django Based on SAE

(2) SAE Python developer Manual

========================================================== ========

This article comes from:Http://blog.csdn.net/zh405123507

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.