Home >Backend Development >Python Tutorial >Turn your old Python web application into WSGI ready and Apache

Turn your old Python web application into WSGI ready and Apache

PHPz
PHPzforward
2024-02-09 15:33:03589browse

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

Question content

I have an ancient web application written in python. It's basically a bunch of .py files. For example:

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)

etc

How to run it behind an apache proxy using mod_wsgi installed in a virtual environment? Now I have a pythong 3.11 virtual environment with mod_wsgi-express 5 installed. I can successfully run test.py using the following command:

mod_wsgi-express start server test.py

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

How do I run my old python application? Do I just wrap each .py file in def application(environ, start_response):? Any help would be greatly appreciated.

Add to:

www.index.html for this application. There is a 24fec841278961956547e81b64721d66. This is how it is currently served.

Additionalii

I can't get the output: when using

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

I get the entire html in the logs, then typeerror: 'nonetype' object is not iterable and an internal server error on the browser.


Correct answer


I should use yield bytes(html) instead of print.

The above is the detailed content of Turn your old Python web application into WSGI ready and Apache. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete