使用 Django 或 Flask 这种框架开发 web app 的时候一般都会用内建服务器开发和调试程序,等程序完成后再移交到生产环境部署。问题是这些内建服务器通常都不支持 HTTPS,我们想在开发的时候就能够使用和测试 HTTPS,不想还没测试就部署到生产环境,所以我们需要内建服务器能支持 HTTPS.
这个问题可以通过一个外部程序 stunnel 来解决,stunnel 的作用是通过 OpenSSL 库对 TCP 会话进行加密,建立起一个安全通道,保护没有加密功能或未加密的程序。其主要功能有两个:
接收未加密的数据流,进行 SSL 加密,然后把加密后的数据流通过网络发送出去;
对已加密的数据流进行解密,并将解密后的数据流其通过网络发送给另一个程序。
了解了 stunnel 的功能后我们很容易就能想到利用 stunnel 建立一个 SSL 加密通道绑定到 Django/Flask 内建服务器上,stunnel 启动 443 端口接受用户的 HTTPS 请求,解密后发送给内建服务器的 8000 端口处理,内建服务器处理完后发送数据给 stunnel 然后加密后返回给浏览器用户。
好吧,上面说了一堆貌似很复杂,其实使用 stunnel 很简单。
在 Django/Flask 开发服务器所在的服务器上安装 stunnel:
# yum install stunnel(在 CentOS 上)
或者
$ sudo apt-get install stunnel4(在 Ubuntu 上)
如果没有购买 SSL 证书的话自己生成一个,对了,这个文件的权限必须是 600 哦:
# openssl req -new -x509 -days 365 -nodes -out vpsee.pem -keyout vpsee.pem # chmod 600 vpsee.pem
新建一个配置文件叫做 https,然后用 stunnel 执行这个配置文件,启动 443 端口连接到 Django/Flask 内建服务器的 8000 端口:
# vi https pid = cert = vpsee.pem debug = 7 foreground = yes [https] accept = 443 connect = 8000 # stunnel https
启动 Django 内建服务器绑定到上面配置文件提到的 8000 端口:
# HTTPS=1 python manage.py runserver 0.0.0.0:8000
启动 Flask 内建服务器不需要特别的,改变端口到 8000,按照正常的方式启动就可以了:
# vi run.py #!flask/bin/python from app import app app.run(host='0.0.0.0', port=8000, debug = True) # ./run.py * Running on http://0.0.0.0:8000/ * Restarting with reloader

Pythonusesahybridapproach,combiningcompilationtobytecodeandinterpretation.1)Codeiscompiledtoplatform-independentbytecode.2)BytecodeisinterpretedbythePythonVirtualMachine,enhancingefficiencyandportability.

ThekeydifferencesbetweenPython's"for"and"while"loopsare:1)"For"loopsareidealforiteratingoversequencesorknowniterations,while2)"while"loopsarebetterforcontinuinguntilaconditionismetwithoutpredefinediterations.Un

In Python, you can connect lists and manage duplicate elements through a variety of methods: 1) Use operators or extend() to retain all duplicate elements; 2) Convert to sets and then return to lists to remove all duplicate elements, but the original order will be lost; 3) Use loops or list comprehensions to combine sets to remove duplicate elements and maintain the original order.

ThefastestmethodforlistconcatenationinPythondependsonlistsize:1)Forsmalllists,the operatorisefficient.2)Forlargerlists,list.extend()orlistcomprehensionisfaster,withextend()beingmorememory-efficientbymodifyinglistsin-place.

ToinsertelementsintoaPythonlist,useappend()toaddtotheend,insert()foraspecificposition,andextend()formultipleelements.1)Useappend()foraddingsingleitemstotheend.2)Useinsert()toaddataspecificindex,thoughit'sslowerforlargelists.3)Useextend()toaddmultiple

Pythonlistsareimplementedasdynamicarrays,notlinkedlists.1)Theyarestoredincontiguousmemoryblocks,whichmayrequirereallocationwhenappendingitems,impactingperformance.2)Linkedlistswouldofferefficientinsertions/deletionsbutslowerindexedaccess,leadingPytho

Pythonoffersfourmainmethodstoremoveelementsfromalist:1)remove(value)removesthefirstoccurrenceofavalue,2)pop(index)removesandreturnsanelementataspecifiedindex,3)delstatementremoveselementsbyindexorslice,and4)clear()removesallitemsfromthelist.Eachmetho

Toresolvea"Permissiondenied"errorwhenrunningascript,followthesesteps:1)Checkandadjustthescript'spermissionsusingchmod xmyscript.shtomakeitexecutable.2)Ensurethescriptislocatedinadirectorywhereyouhavewritepermissions,suchasyourhomedirectory.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
