卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34948本站已运行393

将旧的 Python Web 应用程序转变为 WSGI 就绪和 Apache

将旧的 python web 应用程序转变为 wsgi 就绪和 apache

问题内容

我有一个用 python 编写的古老 web 应用程序。它基本上是一堆 .py 文件。例如:

display.py:

import cgi
import re
import string
import operator
from urllib.parse import urlparse
from errors import herigean
from routines import *

error = false

query = cgifieldstoragetodict(cgi.fieldstorage())
opening_index = 0 # flag to indicate whether we're opening the index page
if ('what' not in query):
        query['what'] = 'index'
if 'fs' not in query:
        query['fs'] = str(default_font_size)

# open page to display
try:
        fil = open('html/'+query['what']+'.fmt')
        textlines = fil.read()
        queryreg = re.compile('query:fs:query')
      textlines = queryreg.sub(query['fs'],textlines)
        fil.close()
except ioerror:
        error = true

if query['what'] == 'about':
        try:
                fil = open('legal/lgpl-3.0.txt')
                lgpl = fil.read()
                fil.close()
                fil = open('legal/gpl.txt')
                gpl = fil.read()
                fil.close()
                fil = open('html/availability.fmt')
                availability = fil.read()
                fil.close()
        except ioerror:
                error = true
if query['what'] == 'corpus':
        try:
                fil = open('html/availability.fmt')
[...]
if error:
        herigean()
else:
        print(frontmatter)

等等

如何使用虚拟环境中安装的 mod_wsgi 在 apache 代理后面运行它?现在我有一个pythong 3.11虚拟环境,其中安装了mod_wsgi-express 5。我可以使用以下命令成功运行test.py:

mod_wsgi-express启动服务器测试.py

def application(environ, start_response):
    start_response('200 ok', [('content-type', 'text/html')])
    return [b'hello, world!']

如何运行我的旧 python 应用程序?我是否只是将每个 .py 文件包装在 def application(environ, start_response): 中?任何帮助将不胜感激。

添加

该应用程序的 www.index.html 。里面有一个 。这就是目前的服务方式。

附加ii

我无法获得输出:使用时

def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html')])
    [...]
    return print(frontmatter)

我在日志中获取了整个 html,然后是 typeerror: 'nonetype' object is not iterable 和浏览器上的内部服务器错误。


正确答案


我应该使用 yield bytes(html) 而不是 print

卓越飞翔博客
上一篇: Go gin:如何读取多个路线段?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏