import socket
server_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1',9998))
server_sk.listen(130)#.listen使套接字变为可以被动链接
def index(path):
msg='这是{}页面'.format(path).encode('utf-8')
return msg
def home(path):
msg='this is {} html'.format(path).encode('utf-8')
return msg
def error(path):
msg='这是一个错误的请求'.encode('utf-8')
return msg
while True:
client,addrs=server_sk.accept()
content=client.recv(1024).decode('utf-8')
print(content)
#首先应该将路径截取出来,利用切片
head_lst=content.split('\r\n')
title=head_lst[0].split(' ')
path=title[1]
if path=='/home':
msg=home(path)
elif path=='/index':
msg=index(path)
else:
msg=error(path)
#给浏览器返回消息
msg0='HTTP/1.1 200 OK\r\n'.encode('utf-8')
msg1='Content-type:text/html;charset=utf-8\r\n'.encode('utf-8')
msg2='\r\n'.encode('utf-8')
client.send(msg0)
client.send(msg1)
client.send(msg2)
client.send(msg)
优化:
import socket
server_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1',9998))
server_sk.listen(130)#.listen使套接字变为可以被动链接
def index(path):
msg='这是{}页面'.format(path).encode('utf-8')
return msg
def home(path):
msg='this is {} html'.format(path).encode('utf-8')
return msg
def error(path):
msg='这是一个错误的请求'.encode('utf-8')
return msg
def html(path):
msg='这是一个HTML网页'.encode('utf-8')
return msg
while True:
client,addrs=server_sk.accept()
content=client.recv(1024).decode('utf-8')
print(content)
#首先应该将路径截取出来,利用切片
head_lst=content.split('\r\n')
title=head_lst[0].split(' ')
path=title[1]
path_list=[
('/index',index),
('/home',home),
('/html',html)
]
func=None
for lis in path_list:
if lis[0]==path:
func=lis[1]
break
if func:
msg=func(path)
else:
msg=error(path)
#给浏览器返回消息
msg0='HTTP/1.1 200 OK\r\n'.encode('utf-8')
msg1='Content-type:text/html;charset=utf-8\r\n'.encode('utf-8')
msg2='\r\n'.encode('utf-8')
client.send(msg0)
client.send(msg1)
client.send(msg2)
client.send(msg)
返回具体的HTML文件:
import socket
def index():
with open('index.html',mode='rb') as f:
msg= f.read()
return msg
def home():
with open('home.html',mode='rb') as f:
msg=f.read()
return msg
def html():
with open('HTML2.html',mode='rb') as f:
msg=f.read()
return msg
def error():
with open('error.html',mode='rb') as f:
msg=f.read()
return msg
server_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1',9997))
server_sk.listen(120)
path_list=[
('/index',index),
('/home',home),
('/html',html)
]
while True:
print('等待客户端连接')
client_sk,addrs=server_sk.accept()
content=client_sk.recv(1024).decode('utf-8')
print(content)
header_lst=content.split('\r\n')
title_lst=header_lst[0].split(' ')
path=title_lst[1]
func=None
for path_tup in path_list:
if path_tup[0]==path:
func=path_tup[1]
break
if func:
msg=func()
else:
msg=error()
client_sk.send('HTTP/1.1 200 OK\r\n'.encode('utf-8')) # 设置响应首行
client_sk.send('Content-Type:text/html;charset=utf-8\r\n'.encode('utf-8')) # 响应头
client_sk.send('\r\n'.encode('utf-8')) # 响应空行
client_sk.send(msg)
# client_sk.send('你好啊'.encode('utf-8')) # 响应内容
print('客户端发来贺电')
client_sk.close()
让网页动起来:
import socket
def index():
with open('index.html',mode='r',encoding='utf-8') as f:
msg= f.read()
msg=msg.replace('姑姑','女朋友').encode('utf-8')
return msg
def home():
with open('home.html',mode='rb') as f:
msg=f.read()
return msg
def html():
with open('HTML2.html',mode='rb') as f:
msg=f.read()
return msg
def error():
with open('error.html',mode='rb') as f:
msg=f.read()
return msg
server_sk=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_sk.bind(('127.0.0.1',9997))
server_sk.listen(120)
path_list=[
('/index',index),
('/home',home),
('/html',html)
]
while True:
print('等待客户端连接')
client_sk,addrs=server_sk.accept()
content=client_sk.recv(1024).decode('utf-8')
print(content)
header_lst=content.split('\r\n')
title_lst=header_lst[0].split(' ')
path=title_lst[1]
func=None
for path_tup in path_list:
if path_tup[0]==path:
func=path_tup[1]
break
if func:
msg=func()
else:
msg=error()
client_sk.send('HTTP/1.1 200 OK\r\n'.encode('utf-8')) # 设置响应首行
client_sk.send('Content-Type:text/html;charset=utf-8\r\n'.encode('utf-8')) # 响应头
client_sk.send('\r\n'.encode('utf-8')) # 响应空行
client_sk.send(msg)
# client_sk.send('你好啊'.encode('utf-8')) # 响应内容
print('客户端发来贺电')
client_sk.close()
(7).总结
总结
1.web框架的本质:socket 服务端 与浏览器的通讯。
2.socket 服务端功能可以划分为3部分:
- a.负责与浏览器收发消息(socket)在python中有专门的框架
wsgiref/uWsgi/gunicorn… - b.根据用户访问不同的路径执行不同的函数
- c.从HTML中读取出内容,并且完成字符串的替换
3.Python中 web 框架的分类: - 按照2上面的功能分类
(1).框架自带a,b,c 功能 ----> Tornado
(2).框架自带b,c,使用第三方的a ---->Django
(3).框架自带b,使用第三方的a,c ---->Flask
-按照另一个维度划分
(1).Diango ,Tornado–>大而全(做一个网站用到的技术都有)
(2).其他 例如 Flask 轻量级只封装了核心功能。
a部分和b、c 部分通讯需要遵守WSGI 协议。