WEB框架原理(socket)
socket服务端
socket示例 import socket sk = socket.socket() sk.bind(('127.0.0.1', 8000)) sk.listen() while 1: conn, addr = sk.accept() ret = conn.recv(10240) print(ret) conn.send(b"HTTP/1.1 200 OK\r\n\r\nooooojiba98k")
不同路劲返回不同的内容 import socket sk = socket.socket() sk.bind(('127.0.0.1', 8000)) sk.listen() while 1: conn, addr = sk.accept() ret = conn.recv(10240) ret_str = str(ret, encoding='utf-8') res = ret_str.split('\r\n')[0].split(' ')[1] print(res) if res == '/aaa/': msg = b'123' elif res == '/bbb/': msg = b'456' else: msg = b'789' conn.send(b'HTTP/1.1 200 OK\r\n\r\n') conn.send(msg) conn.close()
不同路劲返回不同内容(函数版) import socket sk = socket.socket() sk.bind(('127.0.0.1', 8000)) sk.listen() def aaa(res): data = 'What you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def bbb(res): data = 'What you visited was:{}'.format(res) return bytes(data, encoding='utf-8') while 1: conn,addr = sk.accept() ret = conn.recv(10240) ret_str = str(ret, encoding='utf-8') res = ret_str.split('\r\n')[0].split(' ')[1] print(res) if res == '/aaa/': msg = aaa(res) elif res == '/bbb/': msg = bbb(res) else: msg = b'Not found' conn.send(b'HTTP/1.1 200 OK\r\n\r\n') conn.send(msg) conn.close()
不同路径返回不同内容(反射版) import socket sk = socket.socket() sk.bind(('127.0.0.1', 8000)) sk.listen() def aaa(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def bbb(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') res_list = [ ('/aaa/', aaa), ('/bbb/', bbb) ] while 1: # ------连接部分------ conn,addr = sk.accept() ret = conn.recv(10240) # 收到的字节转化成字符串 ret_str = str(ret, encoding='utf-8') # 对请求行进行切割 res = ret_str.split('\r\n')[0].split(' ')[1] # -------业务处理部分-------- # 使用func变量保存将要执行的函数 for i in res_list: if i[0] == res: func = i[1] break else: func = None # 执行函数 if func: msg = func(res) else: msg = b'404Not found' # ----回复响应消息---- conn.send(b'HTTP/1.1 200 OK\r\n\r\n') conn.send(msg) conn.close()
返回网页 import socket sk = socket.socket() sk.bind(('127.0.0.1', 8000)) sk.listen() def aaa(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def bbb(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def login(res): with open('login.html', 'rb')as f: data = f.read() return data res_list = [ ('/aaa/', aaa), ('/bbb/', bbb), ('/login/', login), ] while 1: # ------连接部分------ conn,addr = sk.accept() ret = conn.recv(10240) # 收到的字节转化成字符串 ret_str = str(ret, encoding='utf-8') # 对请求行进行切割 res = ret_str.split('\r\n')[0].split(' ')[1] # -------业务处理部分-------- # 使用func变量保存将要执行的函数 for i in res_list: if i[0] == res: func = i[1] break else: func = None # 执行函数 if func: msg = func(res) else: msg = b'404Not found' # ----回复响应消息---- conn.send(b'HTTP/1.1 200 OK\r\n\r\n') conn.send(msg) conn.close()
返回动态网页 import socket import time sk = socket.socket() sk.bind(('127.0.0.1', 8800)) sk.listen() def aaa(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def bbb(res): data = 'what you visited was:{}'.format(res) return bytes(data, encoding='utf-8') def login(res): with open('login.html', 'r', encoding='utf-8')as f: data = f.read() now = str(time.time()) data = data.replace('@@**@@', now) return bytes(data, encoding='utf-8') res_list = [ ('/aaa/', aaa), ('/bbb/', bbb), ('/login/', login), ] while 1: # ------连接部分------ conn, addr = sk.accept() ret = conn.recv(10240) # 收到的字节转化成字符串 ret_str = str(ret, encoding='utf-8') # 对请求行进行切割 res = ret_str.split('\r\n')[0].split(' ')[1] # -------业务处理部分-------- # 使用func变量保存将要执行的函数 for i in res_list: if i[0] == res: func = i[1] break else: func = None # 执行函数 if func: msg = func(res) else: msg = b'404Not found' # ----回复响应消息---- conn.send(b'HTTP/1.1 200 OK\r\n\r\n') conn.send(msg) conn.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta http-equiv="x-ua-compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> </head> <body> <h1>This is login page!</h1> <p>时间:@@**@@</p> </body> </html>