Turn your old Python web application into WSGI ready and Apache

PHPz
Release: 2024-02-09 15:33:03
forward
541 people have browsed it

将旧的 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)
Copy after login

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!']
Copy after login

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 <meta http-equiv="refresh" content="0;url=display.py?what=index" />. 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)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!