Python app flask+nginx+fastcgi implementing Python application Deployment----the MAC environment

Source: Internet
Author: User

For an iOS developer, writing background apps is not a necessary skill, but mastering a background language can be a complete addition to a more comprehensive understanding of the front and back architectures, while also writing back-Office applications on the fly in real-world development work.

The Flask framework itself integrates a simple server that can be called natively, whereas in this case the Python application interface can only be debugged using emulator, and if you want to debug with a real machine, we still need to deploy the server serious.

Here, we introduce a nginx collocation fastcgi to implement a local server deployment in a Mac environment.

Nginx Configuration
    • A. Installing the Homebrew
-e"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • B. Install nginx

        sudo brew install nginx (remove sudo when error)  
    • C. Start Nginx

       Code>sudo nginx  
    • D. Modify the configuration file (the file is read-only by default and can change read and write permissions in the introduction, right-click menu)

        path:/usr/local/ etc/nginx/nginx.conf  
server {listen 8000 ; // nginx  listening port server_name localhost;    CharSet Utf-8 ;        Location/include  fastcgi_params; Fastcgi_param path_info   $fastcgi _script_name        ;        Fastcgi_param script_name   "" ; Fastcgi_pass 127.0 . 0 . 1  :  8008 ; // set fastcgi application listening port (note the port and the Listen 
    • E. Stop Nginx

FASTCGI configuration
    • Installing fastcgi

       sudo pip install flup
Python app Authoring

The following is a simple flask code example

#!/usr/bin/python fromflup.server.fcgiImportWsgiserver fromFlaskImportFlask, request fromBson.objectidImportObjectIdImportEncrypt def MyApp(environ, start_response):Start_response (' OK ', [(' Content-type ',' Text/plain ')]) def MyApp(environ, start_response):Start_response (' OK ', [(' Content-type ',' Text/plain ')]) (rquest_method,data) = Get_environ (environ)ifrquest_method==' GET ':return ' Get querystr: '+dataElse:return ' POST RequestData '+data def get_environ(environ):Rquest_method = environ["Request_method"] Data="'    ifrquest_method==' GET ': data=environ["Query_string"]Else: data = environ["Wsgi.input"].read ()return(Rquest_method,data)if__name__ = =' __main__ ': Wsgiserver (MyApp, bindaddress= (' 127.0.0.1 ',8008). Run ()//The port here must be the same as the port configured above to enable monitoring
Run
启动Nginx服务器运行Python应用

Here our local server and Python applications are deployed and can be accessed by any browser or client within the LAN. The effect is as follows:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python app flask+nginx+fastcgi implementing Python application Deployment----the MAC environment

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.