Read the File and Return It to the Browser | Teach You How to Get Started With Python One Hundred and Fifteen

This section describes reading a file and returning the result to the browser.

Read the file and return it to the browser


hello.html:

< html lang = " en " >
< head >
< meta charset = "UTF-8" >
Title










< div class = "bottom" >




info.html:
{username} , welcome back, you are {age} years old this year, your gender is {gender}
Read file:
import json
from wsgiref.simple_server import make_server _


def demo_app ( environ, start_response ) :
path = environ[ 'PATH_INFO' ]
# print ( environ. get ( 'QUERY_STRING' )) # QUERY_STRING ==> Get the parameters passed by the client's GET request
# The method of POST request data will be discussed later

status_code = '200 OK'
if path == '/' :
response = 'Welcome to my homepage'
elif path == '/test' :
response = json.dumps ({ 'name' : ' zhangsan ' , 'age' : 18 })
elif path == '/demo' :
with open('pages/xxxx.txt', 'r', encoding='utf8') as file:
response = file.read()
elif path == '/hello':
with open('pages/hello.html', 'r', encoding='utf8') as file:
response = file.read ( )
elif path == '/info' :
# Query the database to get the username
name = 'jack'
with open ( 'pages/info.html' , 'r' , encoding= 'utf8' ) as file :
# '{username}, welcome back' .format(username=name)
# flask django template, rendering engine
response = file . read ().format(username=name, age= 18 , gender= 'male' )
else :
status_code = '404 Not Found'
response = 'The page is lost'

start_response(status_code, [('Content-Type', 'text/html;charset=utf8')])
return [response.encode('utf8')]

if __name__ == '__main__':
httpd = make_server('', 8080, demo_app)
sa = httpd.socket.getsockname ( )
print ( "Serving HTTP on" , sa [ 0 ], "port" , sa [ 1 ], "..." )
httpd.serve_forever ( )

Read the file and return it to the browser.HTTP server optimization


import json
from wsgiref.simple_server import make_server

def load_file(file_name, **kwargs):
try:
with open('pages/' + file_name, 'r', encoding='utf8') as file:
content = file.read()
if kwargs : # kwargs = {'username':'zhangsan','age':19,'gender':'male'}
content = content.format (** kwargs )
# {username}, welcome back, you are {age} years old this year, your gender is {gender}.format(** kwargs )
return content
except FileNotFoundError :
print( 'File not found' )


def demo_app ( environ , start_response ) :
path = environ[ 'PATH_INFO' ]

status_code = '200 OK'
if path == '/' :
response = 'Welcome to my homepage'
elif path == '/test' :
response = json.dumps({'name': 'zhangsan', 'age': 18})
elif path == '/demo':
response = load_file('xxxx.txt')
elif path == '/hello':
response = load_file('hello.html')
elif path == '/info' :
response = load_ file ( 'info.html' , username= ' zhangsan ' , age= 19 , gender= 'male' )
else :
status_code = '404 Not Found'
response = 'The page is lost'

start_response(status_code, [('Content-Type', 'text/html;charset=utf8')])
return [response.encode('utf8')]


if __name__ == '__main__':
httpd = make_server('', 8080, demo_app)
sa = httpd.socket .getsockname ()
print ( "Serving HTTP on" , on [ 0 ], "port" , on [ 1 ], "..." )
httpd.serve _forever ()

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00