Talking about Web Framework
Keywords:
Python
socket
encoding
Django
I. Essence of Web Framework
II. Web Framework Function
-
socket sends and receives messages - wsgiref (test), uwsgi (online)
-
Returns different strings according to different paths
-
Return dynamic pages (string substitution) - jinja2
III. Types of Web Framework
-
django
-
flask
-
tornado
-
socket sends and receives messages
-
Returns different strings according to different paths
-
Return to dynamic page (string substitution)
4. Custom web Framework
import socket
# Create a socket object
sk = socket.socket()
# binding IP And port
sk.bind(('127.0.0.1', 8000))
# Monitor
sk.listen(5)
# function
def index(url):
ret = '<h1>index!</h1>({})'.format(url)
return ret.encode('utf-8')
def home(url):
ret = '<h1>home!</h1>({})'.format(url)
return ret.encode('utf-8')
# Waiting for connection
while True:
conn, addr = sk.accept()
# receive data
data = conn.recv(1024)
data = data.decode('utf-8')
url = data.split()[1]
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
if url == '/index/':
# Return data
ret = index(url)
elif url == '/home/':
ret = home(url)
else:
ret = b'<h1>404 not found!</h1>'
conn.send(ret)
# Disconnect
conn.close()
Function version
import socket
# Create a socket object
sk = socket.socket()
# binding IP And port
sk.bind(('127.0.0.1', 8000))
# Monitor
sk.listen(5)
# function
def index(url):
ret = '<h1>index!</h1>({})'.format(url)
return ret.encode('utf-8')
def home(url):
ret = '<h1>home!</h1>({})'.format(url)
return ret.encode('utf-8')
# Define a list1 Correspondence to the actual function to be executed
list1 = [
('/index/', index),
('/home/', home),
]
# Waiting for connection
while True:
conn, addr = sk.accept()
# receive data
data = conn.recv(1024)
data = data.decode('utf-8')
url = data.split()[1]
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
func = None
for i in list1:
if url == i[0]:
func = i[1]
break
if func:
ret = func(url)
else:
ret = b'<h1>404 not found!</h1>'
conn.send(ret)
# Disconnect
conn.close()
Function Advanced Edition
import socket
# Create a socket object
sk = socket.socket()
# binding IP And port
sk.bind(('127.0.0.1', 8000))
# Monitor
sk.listen(5)
# function
def index(url):
with open('index.html','rb') as f:
ret = f.read()
return ret
def home(url):
ret = '<h1>home!</h1>({})'.format(url)
return ret.encode('utf-8')
# Define a list1 Correspondence to the actual function to be executed
list1 = [
('/index/', index),
('/home/', home),
]
# Waiting for connection
while True:
conn, addr = sk.accept()
# receive data
data = conn.recv(1024)
data = data.decode('utf-8')
url = data.split()[1]
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
func = None
for i in list1:
if url == i[0]:
func = i[1]
break
if func:
ret = func(url)
else:
ret = b'<h1>404 not found!</h1>'
conn.send(ret)
# Disconnect
conn.close()
Return to html page
import socket
import time
# Create a socket object
sk = socket.socket()
# binding IP And port
sk.bind(('127.0.0.1', 8000))
# Monitor
sk.listen(5)
# function
def index(url):
with open('index.html', 'rb') as f:
ret = f.read()
return ret
def home(url):
ret = '<h1>home!</h1>({})'.format(url)
return ret.encode('utf-8')
def timer(url):
now = time.strftime('%H:%M:%S')
with open('time.html','r',encoding='utf-8') as f:
data = f.read()
data = data.replace('xxtimexx',now)
return data.encode('utf-8')
# Define a list1 Correspondence to the actual function to be executed
list1 = [
('/index/', index),
('/home/', home),
('/time/', timer),
]
# Waiting for connection
while True:
conn, addr = sk.accept()
# receive data
data = conn.recv(1024)
data = data.decode('utf-8')
url = data.split()[1]
conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
func = None
for i in list1:
if url == i[0]:
func = i[1]
break
if func:
ret = func(url)
else:
ret = b'<h1>404 not found!</h1>'
conn.send(ret)
# Disconnect
conn.close()
//Supplement: time.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>The current time is:@@time@@</h1>
</body>
</html>
Return to dynamic page