Maison  >  Article  >  développement back-end  >  Transformez votre ancienne application Web Python en compatible WSGI et Apache

Transformez votre ancienne application Web Python en compatible WSGI et Apache

PHPz
PHPzavant
2024-02-09 15:33:03541parcourir

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

Contenu de la question

J'ai une ancienne application Web écrite en python. Il s'agit essentiellement d'un tas de fichiers .py. Par exemple :

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)

Attendez

Comment l'exécuter derrière un proxy Apache en utilisant mod_wsgi installé dans un environnement virtuel ? J'ai maintenant un environnement virtuel pythong 3.11 avec mod_wsgi-express 5 installé. Je peux exécuter avec succès test.py en utilisant la commande suivante :

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

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

Comment exécuter mon ancienne application python ? Dois-je simplement envelopper chaque fichier .py dans def application(environ, start_response): ? Toute aide serait grandement appréciée.

Ajouter  :

L'application est www.index.html 。里面有一个 877b1115d8e4694cad45ad73ff3130ed. C'est ainsi qu'il est actuellement servi.

II supplémentaire

Je n'arrive pas à obtenir le résultat : lors de l'utilisation de

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

Je reçois l'intégralité du code HTML dans les journaux, puis typeerror : l'objet 'nonetype' n'est pas itérable et une erreur de serveur interne sur le navigateur. typeerror: 'nonetype' object is not iterable 和浏览器上的内部服务器错误。


正确答案


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

Bonne réponse

🎜🎜Je devrais utiliser yield bytes(html) au lieu de print. 🎜

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer