To create a simple web server using WSGI and when i'm running this code through this "A server error occurred. Please contact the administrator" in my browser.
My Code
main.py
import os
from wsgiref.simple_server import make_server
def application(environ, start_response):
# Mimetype
ctype = 'text/html'
# Directory
dir = environ["SCRIPT_FILENAME"][:environ["SCRIPT_FILENAME"].rindex("/")]
# Get File Contents
file_contents = b""
with open(dir+"/main.html", "rb") as file:
file_contents = file.read()
# Add Dynamic Content
response_body = b"This is a header!".join(
b"".join(
file_contents.split(b"%(HEAD)")
).split(b"%(HEADING)")
)
# Heading
status = '200 OK'
response_headers = [
('Content-Type', ctype), ('Content-Length', str(len(response_body)))
]
# Send Response
start_response(status, response_headers)
return [response_body.encode('utf-8')]
httpd = make_server('localhost', 8080, application)
# Now it is serve_forever() in instead of handle_request()
httpd.serve_forever()
main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Hello, Python WSGI Application</h1>
</body>
</html>
Both file main.py and main.html contains into the name of "app" directory. I couldn't detect actual problem what happened here.
Thanks.
Aucun commentaire:
Enregistrer un commentaire