标签 python 下的文章

5. Longest Palindromic Substring最长回文子串

解法1:中心拓展算法

思路

首先,我们知道回文串一定是对称的,所以我们可以选择一个对称中心,进行左右扩展,判断左右字符是否相等即可。
由于存在奇数的字符串和偶数的字符串,所以我们需要从一个字符开始扩展,或者从两个字符之间开始扩展,所以总共有 n+n-1 个中心。

实现代码

class Solution:
    def longestPalindrome(self, s: str) -> str:
        n = len(s)
        Max,sub = 0,s[0:1]
        for i in range(n):
            tmp = self.searchPalindrome(i-1,i+1,s)
            if len(tmp) > Max:
                Max = len(tmp)
                sub = tmp
            tmp = self.searchPalindrome(i-1,i,s)
            if len(tmp) > Max:
                Max = len(tmp)
                sub = tmp    
        return sub
    
    def searchPalindrome(self, left: int, right: int, s: str) -> int:
        sub = ""
        while left != -1 and right != len(s):
            if s[left] == s[right]:
                sub = s[left:right+1]
                left-=1
                right+=1
            else : break
        return sub

马拉车算法

class Solution:
    # Manacher 算法
    def longestPalindrome(self, s: str) -> str:
        # 特判 
        if len(s) < 2 or s == s[::-1]:
            return s

        # 得到预处理字符串
        t = "#" + "#".join(s) + "#"

        # 新字符串的长度
        t_len = len(t)

        # 数组 p 记录了扫描过的回文子串的信息
        p = [0]*t_len

        # 双指针,它们是一一对应的,须同时更新
        max_right = 0
        center = 0

        # 当前遍历的中心最大扩散步数,其值等于原始字符串的最长回文子串的长度
        max_len = 1
        # 原始字符串的最长回文子串的起始位置,与 max_len 必须同时更新
        start = 1

        for i in range(t_len):
            if i < max_right:
                mirror = 2 * center - i
                # 这一行代码是 Manacher 算法的关键所在,要结合图形来理解
                p[i] = min(max_right - i, p[mirror])

            # 下一次尝试扩散的左右起点,能扩散的步数直接加到 p[i] 中
            left = i - (1 + p[i])
            right = i + (1 + p[i])

            # left >= 0 and right < t_len 保证不越界
            # t[left] == t[right] 表示可以扩散 1 次
            while left >= 0 and right < t_len and t[left] == t[right]:
                p[i] += 1
                left -= 1
                right += 1

            # 根据 max_right 的定义,它是遍历过的 i 的 i + p[i] 的最大者
            # 如果 max_right 的值越大,进入上面 i < max_right 的判断的可能性就越大,这样就可以重复利用之前判断过的回文信息了
            if i + p[i] > max_right:
                # max_right 和 center 需要同时更新
                max_right = i + p[i]
                center = i

            if p[i] > max_len:
                # 记录最长回文子串的长度和相应它在原始字符串中的起点
                max_len = p[i]
                start = (i - max_len) // 2
        return s[start: start + max_len]

成果

2020-01-19T15:04:32.png

【leetcode】4. Median of Two Sorted Arrays寻找两个有序数组的中位数

我的初次实现

class Solution:
    def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
        newList = nums1 + nums2
        newList.sort()
        result = 0
        if(len(newList)%2 != 0) :
            result = newList[math.ceil(len(newList)/2-1)]
        else:
            index = int(len(newList)/2)
            result = (newList[index] + newList[index-1])/2
        return result

成果

2020-01-16T13:40:45.png

问题

但是我们仔细观察,可以发现这个的时间复杂度是不够的。

题目描述

思路

查找无重复的字符子串,然后滑动窗口

初次解

每次滑动一格窗口

class Solution:
    def isUnique(self, s: str) -> bool:
        for ch in s:
            if s.count(ch) > 1:
                return False
            else:
                continue
        return True
    def lengthOfLongestSubstring(self, s: str) -> int:
        i,j,Max=0,0,0
        j+=1
        while j <= len(s):
            if self.isUnique(s[i:j]):
                print(s[i:j],"is Unique",i,j)
                Max=max(j-i,Max)
                j+=1
            else:
                i+=1
        return Max

成果

第一次优化

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        if(len(s)==1): 
            return 1
        i,j,Max=0,0,0
        while j <= len(s):
            st = s[i:j+1]
            if(j+1 < len(s)):
                index = st.find(s[j+1])
                if index > -1:
                    i+=(index+1)
                j+=1
                Max=max(j-i+1,Max)
            else:
                break
        return Max

成果

2020-01-15T07:27:31.png

【leetcode】2. Add Two Numbers两数相加

描述

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

思路

我看到这个题的第一感觉就是用递归把数获取出来,然后再相加,之后再把得数结构化。问题就被细分为了两个方面:

  1. 加数的提取
  2. 得数的结构化

我的初次实现

class Solution:
    def getStr(self,node: ListNode) -> str:
        if node.next == None:
            return node.val
        else:
            last = self.getStr(node.next)
            return  str(last) + str(node.val)

    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        num1 = self.getStr(l1)
        num2 = self.getStr(l2)
        result = int(num1) + int(num2)
        resultList = list(str(result))
        tmp = ListNode(int(resultList.pop(0)))
        List = tmp
        while resultList:
            tmp = ListNode(int(resultList.pop(0)))
            tmp.next = List
            List = tmp
        return List

成果

成果

改进思路

利用人列竖式算法的方法,计算每一列的值

改进代码

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        # if l1.next == None and l1.val == 0:
        #    return l2
        # if l2.next == None and l2.val == 0:
        #    return l1
        l1_iter = l1
        l2_iter = l2
        Sum = l1_iter.val + l2_iter.val
        carry = 1 if Sum >= 10 else 0
        Sum %= 10
        List = ListNode(Sum)
        l1_iter = l1_iter.next
        l2_iter = l2_iter.next
        Site = List
        while(l1_iter != None and l2_iter != None):
            Sum = l1_iter.val + l2_iter.val + carry
            carry = 1 if Sum >= 10 else 0
            Sum %= 10
            tmp = ListNode(Sum)
            Site.next = tmp
            Site = Site.next
            l1_iter = l1_iter.next
            l2_iter = l2_iter.next
        last = Site
        if(l1_iter == None and l2_iter == None) :
            if(carry == 1):
                tmp = ListNode(carry)
                last.next = tmp
            return List
        else:
            Site = l1_iter if l2_iter == None else l2_iter
            tail = Site
            while(Site != None and carry == 1):
                Sum = Site.val + carry
                Site.val = Sum % 10
                carry = 0 if Sum < 10 else 1
                if(Site.next == None and carry == 1):
                    tmp = ListNode(carry)
                    Site.next = tmp
                    break
                Site = Site.next
            last.next = tail
        return List

成果

Add Two Numbers超越100%的Python用户

安装pip3

sudo apt-get install python3-pip

安装Django

sudo pip3 install django

验证安装

2019-03-26T06:49:38.png

创建项目

django-admin startproject convert

运行

python3 manage.py runserver

2019-03-26T07:53:45.png

简单的python版本管理器: pyenv

pyenv可以让你轻松的在各版本的python环境中切换自如,它是一个简单而又不引人注目并遵循UNIX传统的专用工具。

这个项目是从rbenvruby-buildfork而来, 并且在配合Python的情况下做了适当的修改.

Terminal output example

pyenv能做什么?

  • 让你在用户基础上改变全局Python版本.
  • 支持为每一个项目设立一个Python版本.
  • 允许您使用环境变量覆盖Python版本.
  • 多个python环境中搜索命令,这有助于在Python版本中进行测试 tox.

与pythonbrew和pythonz相比,pyenv不能做什么?

  • 不依赖于Python本身。 pyenv是由纯shell脚本制作的。没有Python的引导问题。
  • 不需要加载到你的shell中。相反,pyenv的shim方法的工作原理是在$ PATH中添加一个目录。
  • 不能管理virtualenv 当然你可以自行创建virtualenv virtualenv或者使用pyenv-virtualenv去自动化构建

工作原理

在较高的层次上,pyenv使用shim拦截Python命令注入PATH的可执行文件, 确定哪个Python版本已由您的应用程序指定,并传递您的命令使用你想要的Python安装版本。

理解PATH(环境变量路径)

当你执行命令,如python或者pip, 你的操作系统会搜索目录列表以查找可执行文件的那个名字.此目录列表位于环境变量中称为PATH, 列表中的每个目录使用用冒号分隔.

PATH.png

PATH中的目录从左到右搜索,因此首先匹配在列表开头的目录中的可执行文件, 然后一次往右匹配。在这个例子中,首先搜索/usr/local/sbin目录,然后搜索/usr/local/bin,然后是/usr/sbin

理解Shims(垫片)

pyenv的工作原理是在你的PATH前面插入一个shims目录,这样一来系统在搜索Python的时候第一个找到的就是pyenv管理的Python环境。这个插到最前面的路径就叫做垫片(shims)

$(pyenv root)/shims:/usr/local/bin:/usr/bin:/bin

通过一个叫做为_rehashing_的进程, pyenv维护shims目录以匹配每个已安装版本的每个Python命令,比如pythonpip等。

垫片是轻量级可执行文件,只是简单地传递命令到pyenv。所以只要安装了pyenv,当你运行时,比如说,pip,你的操作系统将执行以下操作:

  • PATH中搜索名为pip的可执行文件
  • PATH的开头找到名为pip的pyenv垫片
  • 运行名为pip的垫片,然后将命令传递给属于pyenv的pip命令

选择Python版本

执行shims程序时,pyenv会确定要使用的Python版本,并按此以下资源顺序读取:

  1. PYENV_VERSION环境变量(如果指定). 你可以使用pyenv shell 去设置环境变量在你当前shell session.
  2. 当前特定于应用程序的.python-version文件目录(如果有). 您可以使用 pyenv local修改当前目录.python-version文件.
  3. 通过搜索每个上层目录,找到第一个.python-version文件(如果有的话),直到到达文件系统的根目录
  4. 全局$(pyenv root)/version文件. 您可以使用pyenv global 修改这个文件. 如果是该全局文件不存在,pyenv假设您要使用“系统”Python。(换句话说,如果pyenv不在您的PATH中,那么任何版本都会运行.)

NOTE: 您可以同时激活多个版本,甚至包括Python2或Python3的任何版本. 这允许平行使用Python2和Python3,并且需要像tox这样的工具. 例如,要设置你的首次使用的系统Python和Python3的路径(在这个例子中设置为2.7.9和3.4.2),但也可以在你的PATH使用Python 3.3.6,3.2和2.5,首先是pyenv install缺少的版本,然后设置pyenv全局3.3.6 3.2 2.5.这时, 使用pyenv which应该能够找到每个可执行路径, 例如pyenv which python2.5(应该显示$(pyenv root/versions/2.5 /bin/python2.5) 或者pyenv which python3.4(应该显示系统Python3路径). 您还可以指定多个.python-version`文件中的版本,由换行符或任何空格分隔。

定位Python的安装路径

一旦pyenv确定了您的应用程序具有哪个版本的Python, 它将命令传递给相应的Python.

每个Python版本都安装在自己的目录下
$(pyenv root)/versions.

例如,您可能安装了这些版本:

  • $(pyenv root)/versions/2.7.8/
  • $(pyenv root)/versions/3.4.2/
  • $(pyenv root)/versions/pypy-2.4.0/

就pyenv而言,版本名称只是其中的目录名

$(pyenv root)/versions.

管理虚拟环境

有一个叫做pyenv-virtualenv的pyenv插件, 它有很多功能,可帮助pyenv用户管理virtualenv或Anaconda创建的虚拟环境。因为那些虚拟环境的activate脚本依赖于改变shell的$ PATH变量,
它会去hook拦截pyenv的shim样式命令钩子.如果您有计划使用这些虚拟环境,我们建议您安装pyenv-virtualenv。


安装

如果您是macOS,推荐转至installing with Homebrew.

自动化安装器

访问以下项目:
https://github.com/pyenv/pyenv-installer

基于GitHub检出

这可以让你一直保持最新版本的pyenv, 并且fork上游分支的任何变化

  1. 在哪里检出.
    我们建议$HOME/.pyenv (但其实您可以安装在任何地方).

     $ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    
    
  2. Define environment variable PYENV_ROOT to point to the path where
    pyenv repo is cloned and add $PYENV_ROOT/bin to your $PATH for access
    to the pyenv command-line utility.

    $ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    $ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile

    Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
    Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.
    Proxy note: If you use a proxy, export http_proxy and HTTPS_PROXY too.

  3. Add pyenv init to your shell to enable shims and autocompletion.
    Please make sure eval "$(pyenv init -)" is placed toward the end of the shell
    configuration file since it manipulates PATH during the initialization.

    $ echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
    • Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
    • fish note: Use pyenv init - | source instead of eval (pyenv init -).
    • Ubuntu and Fedora note: Modify your ~/.bashrc file instead of ~/.bash_profile.

    General warning: There are some systems where the BASH_ENV variable is configured
    to point to .bashrc. On such systems you should almost certainly put the abovementioned line
    eval "$(pyenv init -)" into .bash_profile, and not into .bashrc. Otherwise you
    may observe strange behaviour, such as pyenv getting into an infinite loop.
    See #264 for details.

  4. Restart your shell so the path changes take effect.
    You can now begin using pyenv.

    $ exec "$SHELL"
  5. Install Python build dependencies before attempting to install a new Python version. The
    pyenv wiki provides suggested installation packages
    and commands for various operating systems.
  6. Install Python versions into $(pyenv root)/versions.
    For example, to download and install Python 2.7.8, run:

    $ pyenv install 2.7.8

    NOTE: If you need to pass configure option to build, please use
    CONFIGURE_OPTS environment variable.

    NOTE: If you want to use proxy to download, please use http_proxy and https_proxy
    environment variable.

    NOTE: If you are having trouble installing a python version,
    please visit the wiki page about
    Common Build Problems

Upgrading

If you've installed pyenv using the instructions above, you can
upgrade your installation at any time using git.

To upgrade to the latest development version of pyenv, use git pull:

$ cd $(pyenv root)
$ git pull

To upgrade to a specific release of pyenv, check out the corresponding tag:

$ cd $(pyenv root)
$ git fetch
$ git tag
v0.1.0
$ git checkout v0.1.0

Uninstalling pyenv

The simplicity of pyenv makes it easy to temporarily disable it, or
uninstall from the system.

  1. To disable pyenv managing your Python versions, simply remove the
    pyenv init line from your shell startup configuration. This will
    remove pyenv shims directory from PATH, and future invocations like
    python will execute the system Python version, as before pyenv.

    pyenv will still be accessible on the command line, but your Python
    apps won't be affected by version switching.

  2. To completely uninstall pyenv, perform step (1) and then remove
    its root directory. This will delete all Python versions that were
    installed under $(pyenv root)/versions/ directory:

    rm -rf $(pyenv root)

    If you've installed pyenv using a package manager, as a final step
    perform the pyenv package removal. For instance, for Homebrew:

      brew uninstall pyenv
    

Homebrew on macOS

You can also install pyenv using the Homebrew
package manager for macOS.

$ brew update
$ brew install pyenv

To upgrade pyenv in the future, use upgrade instead of install.

Then follow the rest of the post-installation steps under Basic GitHub Checkout above, starting with #3 ("Add pyenv init to your shell to enable shims and autocompletion").

Advanced Configuration

Skip this section unless you must know what every line in your shell
profile is doing.

pyenv init is the only command that crosses the line of loading
extra commands into your shell. Coming from rvm, some of you might be
opposed to this idea. Here's what pyenv init actually does:

  1. Sets up your shims path. This is the only requirement for pyenv to
    function properly. You can do this by hand by prepending
    $(pyenv root)/shims to your $PATH.
  2. Installs autocompletion. This is entirely optional but pretty
    useful. Sourcing $(pyenv root)/completions/pyenv.bash will set that
    up. There is also a $(pyenv root)/completions/pyenv.zsh for Zsh
    users.
  3. Rehashes shims. From time to time you'll need to rebuild your
    shim files. Doing this on init makes sure everything is up to
    date. You can always run pyenv rehash manually.
  4. Installs the sh dispatcher. This bit is also optional, but allows
    pyenv and plugins to change variables in your current shell, making
    commands like pyenv shell possible. The sh dispatcher doesn't do
    anything crazy like override cd or hack your shell prompt, but if
    for some reason you need pyenv to be a real script rather than a
    shell function, you can safely skip it.

To see exactly what happens under the hood for yourself, run pyenv init -.

Uninstalling Python Versions

As time goes on, you will accumulate Python versions in your
$(pyenv root)/versions directory.

To remove old Python versions, pyenv uninstall command to automate
the removal process.

Alternatively, simply rm -rf the directory of the version you want
to remove. You can find the directory of a particular Python version
with the pyenv prefix command, e.g. pyenv prefix 2.6.8.


Command Reference

See COMMANDS.md.


Environment variables

You can affect how pyenv operates with the following settings:

namedefaultdescription
PYENV_VERSION Specifies the Python version to be used.
Also see pyenv shell
PYENV_ROOT~/.pyenvDefines the directory under which Python versions and shims reside.
Also see pyenv root
PYENV_DEBUG Outputs debug information.
Also as: pyenv --debug <subcommand>
PYENV_HOOK_PATHsee wikiColon-separated list of paths searched for pyenv hooks.
PYENV_DIR$PWDDirectory to start searching for .python-version files.
PYTHON_BUILD_ARIA2_OPTS Used to pass additional parameters to aria2.
if aria2c binary is available on PATH, pyenv use aria2c instead of curl or wget to download the Python Source code. If you have an unstable internet connection, you can use this variable to instruct aria2 to accelerate the download.
In most cases, you will only need to use -x 10 -k 1M as value to PYTHON_BUILD_ARIA2_OPTS environment variable

Development

The pyenv source code is [hosted on
GitHub](https://github.com/pyenv/pyenv). It's clean, modular,
and easy to understand, even if you're not a shell hacker.

Tests are executed using Bats:

$ bats test
$ bats/test/<file>.bats

Please feel free to submit pull requests and file bugs on the [issue
tracker](https://github.com/pyenv/pyenv/issues).

Version History

See CHANGELOG.md.

License

The MIT License

本文环境

操作系统: Ubuntu 16.04.3
Python版本: 3.5.2
Django版本: 2.0.4
nginx版本: 1.10.3

本文平台为腾讯云1核1G系统
我的项目文件名为: dgutpsy

安装pip3(python3 的pip)

sudo apt-get install python3-pip

2018-04-10-20-07-25.png

安装成功后运行pip3将会出现

2018-04-10-20-09-19.png

安装uwsgi

pip install uwsgi

2018-04-10-20-17-19.png

测试uwsgi运行状态

新建文件test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]
有些教程说是 return "hello world"
但是这样其实会出现访问空白的情况, 我的是Python3的环境,需要对hello world进行编码.
而如果你是Python2的环境,你应该写return "hello world"

使用uwsgi运行该文件

uwsgi --http :8000 --wsgi-file test.py
此语句的意思是,使用uwsgi运行test.py文件, 采用http模式, 端口8000

2018-04-10-20-44-49.png

访问页面

好啦,可以看到亲切的hello world 就说明uwsgi运行成功了

2018-04-10-20-39-21.png

TIPS: 如果你访问不了,请先检查腾讯云安全组端口是否开放8000端口

安装Django

pip3 install Django
如在python3里面import django没有报错则安装成功.

2018-04-10-20-55-49.png

上传项目代码

现在我们开始上传项目代码

2018-04-10-20-42-25.png

使用MobaXterm的sftp工具拖进来,[笑着哭]不要问我为什么不用ftp,这个方便.

确认代码运行正常

切进项目目录,运行
python3 manage.py runserver 0.0.0.0:8000

2018-04-10-20-59-03.png

访问页面

2018-04-10-21-01-32.png

不太对?这是因为设置了ALLOWED_HOSTS的原因
我们在setting.py里设置一下
ALLOWED_HOSTS = ['*']

再次访问页面

2018-04-10-21-05-34.png

OK!

uswgi运行项目

 uwsgi --http :8000 --chdir /home/ubuntu/dgutpsy  --wsgi dgutpsy.wsgi
这个语句的意思与上相似,只不过是多了一个工作路径和wsgi文件
2018-04-10-21-05-34.png
开什么玩笑? 你觉得没变化?

至此

我们可以暂定以下内容
  1. 项目没问题.
  2. 项目配置没问题
  3. uwsgi没问题
  4. 对项目的支持环境没问题

接下来, 我们开始配置nginx

安装nginx

sudo apt-get install nginx

测试nginx运行状态

2018-04-10-21-18-53.png

nginx 安装成功

修改nginx配置

文件路径"/etc/nginx/sites-enabled/default"

别再费力找nginx.conf啦,新版本已经没有这个文件的.

我的
upstream django {
        server 127.0.0.1:8001; #web的socket端口
    }
server {
    listen 80 default_server;
    listen [::]:80 default_server;
 
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        root /home/ubuntu/dgutpsy; #项目目录
        uwsgi_pass django;
        include /home/ubuntu/dgutpsy/uwsgi_params; #uwsgi_params文件的地址
    }
}

完整的uwsgi_params文件内容应该是

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;
没有这个文件,或者这个文件写错是会报错的哦.

访问测试502

2018-04-10-21-37-49.png

这是因为我们的socket通信还没开始呢!

nginx 与 uwsgi通信

先使用hello world测试
uwsgi --socket :8001 --wsgi-file test.py

访问测试页面

图片描述


至此: nginx 与 uwsgi通信成功!

TIPS: 这里你是无法通过在浏览器里面访问8001端口来判断是否启动的,因为它是socket协议

让uwsgi后台运行

这样虽然看似可以, 但是当你关闭ssh时,又会出现502了!

这是因为当你关闭这个ssh进程时,uwsgi进程也被终止了.
而且,每次启动都要输一串好长的命令,好不麻烦!

这个时候,我们需要用到一个东西, 那就是ini配置文件启动.
其中daemonize = /home/ubuntu/dgutpsy/test.log的意思就是后台运行并规定日志输出目录。

新建test.ini

[uwsgi]
socket = 127.0.0.1:8001
wsgi-file = /home/ubuntu/dgutpsy/test.py
daemonize = /home/ubuntu/dgutpsy/test.log 
uwsgi --ini test.ini

niginx与Django项目通信

新建dgutpsy.ini

[uwsgi]
socket = 127.0.0.1:8001
chdir           = /home/ubuntu/dgutpsy
module          = dgutpsy.wsgi
master          = true
processes       = 1 

threads = 2 
max-requests = 6000

daemonize = /home/ubuntu/dgutpsy/run.log

然后运行

uwsgi --ini dgutpsy.ini

域名映射

添加域名>解析consult.psyannabel.cn

访问页面

后记:

Q&A:
Q: 能用manage.py运行的,为什么要用uwsgi?

A: 单进程的manage.py, 能和web服务器uwsgi比ma

Q: 能用uwsgi的,为什么还要用nginx?

A: uwsgi虽然是web服务器, 但是它在处理静态文件时, 并没有nginx来得优秀.

Q: 大致流程?

A: nginx运行web处理http请求, nginx使用socket与uwsgi通信,将动态部分转让交给uwsgi处理

Q: 为啥我的test.py运行不了?

A: 如果端口开放没有问题,那么请检查Python版本对应的return