Python web学习:Django + uWSGI + Nginx

2024-04-25 03:48

本文主要是介绍Python web学习:Django + uWSGI + Nginx,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、背景

由于工作需要,最近学习了下Python web的开发,现在做一个简要总结。
选用的Python web架构如下:
Python web框架:Django
应用服务器: uWSGI
代理服务器:Nginx
其中Django是最为常用的Python web框架,类似的框架还有:Flask、web2py等。

二、Django

安装Django可在网上搜索资料。

django-admin

安装完Django后,运行django-admin,得到如下结果:
在这里插入图片描述

创建Python web工程

运行如下命令django-admin startproject HelloWorld,可创建名为HelloWorld的Python web工程,目录结构如下:
在这里插入图片描述

目录说明:
  • HelloWorld: 项目的根目录。
  • manage.py: 一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互,例如通过命令行启动该web项目,通过pycharm启动该web项目等。
  • HelloWorld/init.py: 一个空文件,告诉 Python 该目录是一个 Python 包。
  • HelloWorld/settings.py: 该 Django 项目的设置/配置。
  • HelloWorld/urls.py: 该 Django 项目的 URL 声明; 该web项目提供的所有接口均在这里配置。
  • HelloWorld/wsgi.py: 一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
web项目启动

命令行窗口下执行如下命令:

python manage.py runserver 0.0.0.0:8000

0.0.0.0 让其它电脑可连接到开发服务器,8000 为端口号。如果不说明,那么端口号默认为 8000。

在这里插入图片描述
打开浏览器,输入http://localhost:8000/查看效果:
在这里插入图片描述

三、uWSGI

uWSGI是一个应用服务器,可以理解为地位和Tomcat类似,一般和Nginx配合使用,以提供HTTP服务。

uWSGI使用示例
The first WSGI application

Let’s start with a simple “Hello World” example:

def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return [b"Hello World"]

(save it as foobar.py).

As you can see, it is composed of a single Python function. It is called “application” as this is the default function that the uWSGI Python loader will search for (but you can obviously customize it).

Deploy it on HTTP port 9090

Now start uWSGI to run an HTTP server/router passing requests to your WSGI application:

uwsgi --http :9090 --wsgi-file foobar.py

打开浏览器输入http://localhost:9090/,结果如下
在这里插入图片描述

Adding concurrency and monitoring

The first tuning you would like to make is adding concurrency (by default uWSGI starts with a single process and a single thread).

You can add more processes with the --processes option or more threads with the --threads option (or you can have both).

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

This will spawn 4 processes (each with 2 threads), a master process (will respawn your processes when they die) and the HTTP router (seen before).

uWSGI配置

仍以上面的配置为例:

uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2

Argh! What the hell is this?! dealing with such long command lines is unpractical, foolish and error-prone. Never fear! uWSGI supports various configuration styles. In this quickstart we will use .ini files.

[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
A lot better!

Just run it:

uwsgi yourfile.ini

在浏览器中访问,仍可得到正确结果。

uWSGI详细资料详见本文末尾中的资料链接。

四、Nginx

配置基本和Java web中的Nginx的配置相同,配置文件如下:


# proxy conf
user                        admin;worker_rlimit_nofile        100000;error_log                   "/data/logs/error_log" warn;
pid                         /home/admin/logs/tengine.pid;events {use                     epoll;worker_connections      20480;
}http {include                 mime.types;default_type            application/octet-stream;root                    /home/admin/htdocs;sendfile                on;tcp_nopush              on;server_tokens           off;keepalive_timeout       0;client_header_timeout   1m;send_timeout            1m;client_max_body_size    3m;error_page              506 http://your_error_page.html;index                   index.html index.htm;log_format              proxyformat    "$remote_addr $request_time_usec $http_x_readtime [$time_local] \"$request_method http://$host$request_uri\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$md5_encode_cookie_unb\" \"$md5_encode_$cookie_cookie2\" ";access_log              "/data/logs/access_log" proxyformat;log_not_found           off;gzip                    on;gzip_http_version       1.0;gzip_comp_level         6;gzip_min_length         1024;gzip_proxied            any;gzip_vary               on;gzip_disable            msie6;gzip_buffers            96 8k;gzip_types              text/xml text/plain text/css application/javascript application/x-javascript application/rss+xml;proxy_set_header        Host $host;proxy_set_header        X-Real-IP $remote_addr;proxy_set_header        Web-Server-Type nginx;proxy_set_header        WL-Proxy-Client-IP $remote_addr;proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;proxy_redirect          off;proxy_buffers           128 8k;proxy_intercept_errors  on;# fight mhtml/utf-7 bughat_content             "\r\n";hat_types               text/html text/css;server {listen              8080;server_name         localhost;location /test/tools {include uwsgi_params;uwsgi_pass 127.0.0.1:8000;chunked_transfer_encoding off;}}
}

其中include 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  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;

include uwsgi_params的作用如下:
在这里插入图片描述

五、资料

Django
  1. https://docs.djangoproject.com/zh-hans/2.2/
  2. https://www.jianshu.com/p/679dee0a4193
  3. http://www.runoob.com/django/django-first-app.html
uWSGI
  1. https://uwsgi-docs.readthedocs.io/en/latest/
PEP 3333 – Python Web Server Gateway Interface v1.0.1
  1. https://www.python.org/dev/peps/pep-3333/

这篇关于Python web学习:Django + uWSGI + Nginx的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/933622

相关文章

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Python Websockets库的使用指南

《PythonWebsockets库的使用指南》pythonwebsockets库是一个用于创建WebSocket服务器和客户端的Python库,它提供了一种简单的方式来实现实时通信,支持异步和同步... 目录一、WebSocket 简介二、python 的 websockets 库安装三、完整代码示例1.

揭秘Python Socket网络编程的7种硬核用法

《揭秘PythonSocket网络编程的7种硬核用法》Socket不仅能做聊天室,还能干一大堆硬核操作,这篇文章就带大家看看Python网络编程的7种超实用玩法,感兴趣的小伙伴可以跟随小编一起... 目录1.端口扫描器:探测开放端口2.简易 HTTP 服务器:10 秒搭个网页3.局域网游戏:多人联机对战4.

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.