0
点赞
收藏
分享

微信扫一扫

【Web开发】Python实现Web服务器(Tornado+flask+nginx)


文章目录

  • ​​1、Tornado​​
  • ​​1.1 Tornado简介​​
  • ​​1.2 Tornado安装​​
  • ​​2、openssl​​
  • ​​2.1 下载​​
  • ​​2.2 生成证书​​
  • ​​2.3 浏览器edge中添加证书​​
  • ​​3、nginx​​
  • ​​3.1 nginx简介​​
  • ​​3.2 nginx安装​​
  • ​​3.3 nginx设置ssl​​
  • ​​4、flask​​
  • ​​5、代码实现​​
  • ​​5.1 Tornado​​
  • ​​5.2 Tornado+https​​
  • ​​5.3 Tornado+nginx​​
  • ​​5.4 Tornado(多个)+nginx​​
  • ​​5.5 flask​​
  • ​​5.6 flask+https​​
  • ​​5.7 Tornado+flask​​
  • ​​5.8 Tornado+flask+nginx​​
  • ​​结语​​

1、Tornado

1.1 Tornado简介

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_python

Tornado 是一个python web框架和异步网络库,最初开发于 FriendFeed . 通过使用非阻塞网络I/O,Tornado可以扩展到数万个开放连接,使其非常适合 long polling , WebSockets 以及其他需要与每个用户建立长期连接的应用程序。

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_02

1.2 Tornado安装

pip install

Tornado是为类Unix平台设计的,在系统支持方面具有最佳的性能和可扩展性。 epoll (Linux) kqueue (BSD/MACOS),或 /dev/poll (索拉里斯)

Tornado也将在Windows上运行,尽管该配置没有正式支持或推荐用于生产。Windows上缺少一些功能(包括多进程模式),并且扩展性有限(即使Tornado是建立在 asyncio 支持Windows的Tornado不使用Windows上可扩展网络所需的API)。

2、openssl

2.1 下载

(1)OpenSSL*下载地址:​​https://oomake.com/download/openssl​​ 这个链接有Windows版和源码版最新版下载地址,可以满足Windows、LInux、Mac OS系统使用。

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_03


(2)或者在​​https://slproweb.com/products/Win32OpenSSL.html​​

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_04


安装目录截图如下:

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_python_05

2.2 生成证书

  • (1)新建文件夹ssl,创建私钥
    执行命令如下:

openssl genrsa -des3 -out test.key 1024

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_06


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_07


注意:这里会让你给私钥设置一个密码,随便设置一个,比如:123456

需要输入两次。

  • (2)去除之前创建私钥设置的密码
    不然配置到nginx后启动还要输入密码
    首先复制上面的文件test.key,改名为:test_old.key
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_08

  • 执行命令如下:

openssl rsa -in test_old.key -out test.key

注意过程中需要输入上面密码:123456

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_09

  • (3)创建csr文件

openssl req -new -key test.key -out test.csr

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_10


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_11

  • (4)创建生成crt证书

openssl x509 -req -days 365

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_12


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_python_13

2.3 浏览器edge中添加证书

浏览器访问相关https网址时会先显示提示信息,这是由于浏览器还没有相关证书的原因。

进入浏览器设置找到「证书管理」,在「受信任的根证书颁发机构」添加之前生成的CA证书(不是server证书)。

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_14


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_15

3、nginx

3.1 nginx简介

​​https://nginx.org/en/​​ nginx [engine x] 是 HTTP 和反向代理服务器、邮件代理服务器和通用 TCP/UDP 代理服务器,最初由Igor Sysoev编写。很长一段时间以来,它一直在许多负载重的俄罗斯网站上运行,包括 Yandex、 Mail.Ru、 VK和 Rambler。根据 Netcraft 的数据, 2022 年 5 月,nginx 服务或代理了 21.67% 最繁忙的站点。


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_16

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_17

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_18

  • 提供静态和 索引 文件, 自动索引; 打开文件描述符缓存;
  • 带缓存的加速反向代理; 负载均衡和容错;
  • 加速支持 FastCGI、 uwsgi、 SCGI和 memcached 服务器的缓存; 负载均衡和容错;
  • 模块化架构。过滤器包括 gzipping、字节范围、分块响应、 XSLT、 SSI和图像转换过滤器。如果它们由代理或 FastCGI/uwsgi/SCGI 服务器处理,则可以并行处理单个页面中的多个 SSI 包含;
  • SSL 和 TLS SNI 支持;
  • 支持 具有加权和基于依赖的优先级 的HTTP/2 。

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_19

3.2 nginx安装

​​https://nginx.org/en/download.html​​

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_20


下载之后直接解压就可以使用了。

3.3 nginx设置ssl

worker_processes  1;

error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;

pid logs/nginx.pid;

events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

#HTTPS server

server {
listen 443 ssl;
server_name localhost;

ssl_certificate ./ssl/test.crt;
ssl_certificate_key ./ssl/test.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root html;
index index.html index.htm;
}
}
}

  • 运行结果:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_nginx_21


  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_22

4、flask

详细内容请参考本人的另外几篇文章:

  1. 🎈​​【Web开发】Python实现Web服务器(Flask入门)🎈​​
  2. 🎈​​【Web开发】Python实现Web服务器(Flask测试用例)🎈​​

5、代码实现

5.1 Tornado

  • Python代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.ioloop
import tornado.web
import tornado.options
from tornado.options import options, define, parse_command_line
import platform
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

define('port', type=int, default=8888)

class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello World,爱看书的小沐(tornado),2022!" + "<br>port: " + str(options.port))

if __name__ == "__main__":
tornado.options.parse_command_line()
app = tornado.web.Application([(r"/", MainHandler)])
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
tornado.ioloop.IOLoop.current().start()

  • 运行结果:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_23

5.2 Tornado+https

  • Python代码如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
from tornado.httpserver import HTTPServer
from tornado.web import Application, RequestHandler
from tornado.ioloop import IOLoop
import time

import platform
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

class TestHandler(RequestHandler):
def get(self):
self.write("Hello World,爱看书的小沐!<br>" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) )

settings = {
"static_path" : os.path.join(os.path.dirname(__file__), "static"),
}

application = Application([
(r"/", TestHandler),
], **settings)

if __name__ == "__main__":
server = HTTPServer(application,ssl_options={
"certfile": os.path.join(os.path.abspath("."), "./static/ssl/test.crt"),
"keyfile": os.path.join(os.path.abspath("."), "./static/ssl/test.key"),
})
server.listen(8888)
IOLoop.instance().start()

  • 运行结果:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_python_24

5.3 Tornado+nginx

基于上面“5.1 Tornado”的基础上,在最外面加一个nginx。可以通过修改nginx配置文件:

  • nginx.conf:

server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:8888;
}

或者

{
listen 80;
server_name localhost;
#charset koi8-r;
charset utf-8;
#access_log logs/host.access.log main;

location / {
root d:/myflask/src;
#index index.html index.htm;
proxy_pass http://localhost:8888;
}
}

添加proxy_pass行,其用途为将localhost:80请求全部转发到localhost:8888,也就是tornado监听的端口。

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_25

5.4 Tornado(多个)+nginx

有时候采用nginx + 多个tornado方案。
这里也是基于上面“5.1 Tornado”的基础上,增加nginx,主要是修改nginx的配置文件。

  • 修改配置文件nginx.conf:

worker_processes  1;
events {
worker_connections 1024;
}
http {
upstream frontends {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;

location ^~ /static/ {
root D:\PythonProject\django_web;
if ($query_string) {
expires max;
}
}
location ^~ /media/ {
root D:\PythonProject\django_web;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
rewrite (.*) /static/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/robots.txt;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends;
}
}
}

  • (1)运行tornado服务

python test_tornado.py --port=8000
python test_tornado.py --port=8001
python test_tornado.py --port=8002
python test_tornado.py --port=8003

  • (2)运行nginx.exe启动程序

cd ..
cd ..
cd

nginx.exe -s quit       #停止服务
nginx.exe -s stop #停止服务
nginx.exe -s reload #重启服务

  • (3)浏览器访问http://127.0.0.1
    启动后在浏览器中输入http://127.0.0.1或http://localhost后,即显示nginx欢迎画面。

运行结果如下:

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_26


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_27


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_28


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_29


【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_30

5.5 flask

  • Python代码如下:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World,爱看书的小沐(flask),2022!<br>' + "<font color=blue>花间一壶酒,独酌无相亲。举杯邀明月,对影成三人。</font>"

if __name__ == '__main__':
app.run(debug=True)

  • 运行结果如下:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_31

  • 详细内容请参考本人的另外几篇文章:
  1. 🎈​​【Web开发】Python实现Web服务器(Flask入门)🎈​​
  2. 🎈​​【Web开发】Python实现Web服务器(Flask测试用例)🎈​​

5.6 flask+https

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World,爱看书的小沐(flask),2022!<br>' + "<font color=blue>云想衣裳花想容,春风拂槛露华浓。若非群玉山头见,会向瑶台月下逢。</font>"

# if __name__ == '__main__':
# app.run(debug=True)

if __name__ == '__main__':
app.run(debug=True, ssl_context=(
"static/ssl/test.crt",
"static/ssl/test.key")
)

  • 运行结果如下:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_web server_32


  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_33

5.7 Tornado+flask

Tornado 是一个开源的可伸缩的、非阻塞式的 web 服务器和工具集,它驱动了 FriendFeed 。因为它使用了 epoll 模型且是非阻塞的,它可以处理数以千计的并发固定连接,这意味着它对实时 web 服务是理想的。把 Flask 集成这个服务是直截了当的:

pip install tornado
pip install

如果flask安装不成功,可升级pip。其命令为python -m pip install -U pip

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from yourapplication import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()

  • test_flask.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World,爱看书的小沐,2022!'

if __name__ == '__main__':
app.run(debug=True)

  • test_tornado.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

import platform
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

#导入flask项目
from test_flask import app

# (1)listen:简单的单进程:
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)#对应flask的端口
IOLoop.instance().start()

#(2)bind/ start:简单的多进程:
# 如果要开启多进程模式用下面的代码,不过仅在linux下
# http_server = HTTPServer(WSGIContainer(app))
# http_server.bind(8888)
# http_server.start(0)
# IOLoop.instance().start()

或者

# coding=utf-8
from tornado.httpserver import HTTPServer
from tornado.wsgi import WSGIContainer
#from src.app import app
from test_flask import app
from tornado.ioloop import IOLoop

s = HTTPServer(WSGIContainer(app))
s.listen(5000)
IOLoop.current().start()

  • 运行结果如下:
  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_34


  • 【Web开发】Python实现Web服务器(Tornado+flask+nginx)_tornado_35

5.8 Tornado+flask+nginx

使用nginx web服务器,tornado充当wsgi,tornado负责监听5000端口,转发flask应用。
基于上面“5.3 Tornado+nginx”和“5.7 Tornado+flask”的基础上实现。
这里写上完整的代码如下:

  • flask的代码如下(test_flask.py):

#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello World,爱看书的小沐(flask),2022!<br>' + "<font color=blue>云想衣裳花想容,春风拂槛露华浓。若非群玉山头见,会向瑶台月下逢。</font>"

if __name__ == '__main__':
app.run(debug=True)

  • tornado的代码如下(test_tornado.py):

#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

import platform
if platform.system() == "Windows":
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

#导入flask项目
from test_flask import app

server = HTTPServer(WSGIContainer(app))
server.listen(5000)#对应flask的端口
IOLoop.instance().start()

  • (1)nginx的代码如下(访问http):

server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
proxy_pass http://localhost:5000;
}

http://localhost

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_36

  • (1)nginx的代码如下(访问https):

server {
listen 443 ssl;
server_name localhost;

ssl_certificate ./ssl/test.crt;
ssl_certificate_key ./ssl/test.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
#root html;
#index index.html index.htm;
proxy_pass http://localhost:5000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}

https://localhost

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_flask_37

结语

​如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;​​​╮( ̄▽ ̄)╭
​​​如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;​​​o_O???
​​​如果您需要相关功能的代码定制化开发,可以留言私信作者;​​​(✿◡‿◡)
​​​感谢各位童鞋们的支持!​​( ´ ▽´ )ノ ( ´ ▽´)っ!!!

【Web开发】Python实现Web服务器(Tornado+flask+nginx)_python_38


举报

相关推荐

0 条评论