60、django入门1
1、web的祖宗
""" 先启动服务,监听 while 1: 等待连接... 3. 接收到你的请求 4. 根据你的需求,返回相应的内容 断开 """ import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 print(data) print("=" * 120) conn.send(b'HTTP/1.1 200 OK\r\n\r\n') # 因为要遵循HTTP协议 conn.send(b'O98K') conn.close()
2、根据url的路径跳转不同的页面
""" 根据URL中不同的路径返回不同的内容 """ import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 # print(data) #打印接收到内容 # 从data中取到路径 data = str(data, encoding="utf8") # 把收到的字节类型的数据转换成字符串 # print(data) # 按\r\n分割 data1 = data.split("\r\n")[0] # print(data1) url = data1.split()[1] #切割出url print("url:", url) print("*" * 120) conn.send(b'HTTP/1.1 200 OK\r\n\r\n') # 因为要遵循HTTP协议 # 根据不同的路径返回不同内容 if url == "/index/": response = b"index" elif url == "/home/": response = b"home" else: response = b"404 not found!" conn.send(response) conn.close()
3、根据url,把要返回的内容(也就是发送回客户端的内容)写成函数,根据url的不同,执行不同的函数
""" 根据URL中不同的路径返回不同的内容 """ import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 # 将返回不同的内容部分封装成函数 def index(url): s = "这是{}页面!".format(url) return bytes(s, encoding="utf8") def home(url): s = "这是{}页面!".format(url) return bytes(s, encoding="utf8") while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 # 从data中取到路径 data = str(data, encoding="utf8") # 把收到的字节类型的数据转换成字符串 # 按\r\n分割 data1 = data.split("\r\n")[0] url = data1.split()[1] print("url:", url) print("*" * 120) conn.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n') # 因为要遵循HTTP协议 # 根据不同的路径返回不同内容 if url == "/index/": response = index(url) elif url == "/home/": response = home(url) else: response = b"404 not found!" # 具体的响应体 conn.send(response) conn.close()
4、把所有要执行的函数放到列表中,根据url路径,对列表进行循环,查找到对应的url路径,然后执行元祖中的对应的函数。
""" 根据URL中不同的路径返回不同的内容 函数进阶版 """ import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 # 将返回不同的内容部分封装成函数 def index(url): s = "这是{}页面!".format(url) return bytes(s, encoding="utf8") def home(url): s = "这是{}页面!".format(url) return bytes(s, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ] while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 # 从data中取到路径 data = str(data, encoding="utf8") # 把收到的字节类型的数据转换成字符串 # 按\r\n分割 data1 = data.split("\r\n")[0] url = data1.split()[1] print("url:", url) print("*" * 120) conn.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n') # 因为要遵循HTTP协议 # 根据不同的路径返回不同内容 func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" # 具体的响应体 conn.send(response) conn.close()
5、根据url中不同的路径返回不同的内容,把每个函数中要返回的内容变成文档,对文档进行读取然后返回。
""" 根据URL中不同的路径返回不同的内容 函数进阶版 返回HTML页面 """ import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 # 将返回不同的内容部分封装成函数 def index(url): with open("index.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") def home(url): with open("home.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ] while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 # 从data中取到路径 data = str(data, encoding="utf8") # 把收到的字节类型的数据转换成字符串 # 按\r\n分割 data1 = data.split("\r\n")[0] url = data1.split()[1] print("url:", url) print("*" * 120) conn.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n') # 因为要遵循HTTP协议 # 根据不同的路径返回不同内容 func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" # 具体的响应体 conn.send(response) conn.close()
6、要读取的文档的某些内容被替换,变成动态的
""" 根据URL中不同的路径返回不同的内容 函数进阶版 返回HTML页面 动态起来 """ import socket import time sk = socket.socket() sk.bind(("127.0.0.1", 8080)) # 绑定IP和端口 sk.listen() # 监听 # 将返回不同的内容部分封装成函数 def index(url): with open("index.html", "r", encoding="utf8") as f: s = f.read() now = str(time.time()) s = s.replace("@@oo@@", now) #替换内容 return bytes(s, encoding="utf8") def home(url): with open("home.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ] while 1: # 等待连接 conn, add = sk.accept() data = conn.recv(8096) # 接收客户端发来的消息 # 从data中取到路径 data = str(data, encoding="utf8") # 把收到的字节类型的数据转换成字符串 # 按\r\n分割 data1 = data.split("\r\n")[0] url = data1.split()[1] print("url:", url) print("*" * 120) conn.send(b'HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n') # 因为要遵循HTTP协议 # 根据不同的路径返回不同内容 func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" # 具体的响应体 conn.send(response) conn.close()
7、把响应的内容简化成python中已经封装好的模块(wsgiref模块),直接使用
""" 根据URL中不同的路径返回不同的内容 函数进阶版 返回HTML页面 动态起来 wsgiref模块版 """ import time from wsgiref.simple_server import make_server # 将返回不同的内容部分封装成函数 def index(url): with open("index.html", "r", encoding="utf8") as f: s = f.read() now = str(time.time()) s = s.replace("@@oo@@", now) return bytes(s, encoding="utf8") def home(url): with open("home.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ] def run_server(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ]) # 设置HTTP响应的状态码和头信息 url = environ['PATH_INFO'] # 取到用户输入的url func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" return [response, ] if __name__ == '__main__': httpd = make_server('127.0.0.1', 8090, run_server) print("Serving HTTP on port 8090...") httpd.serve_forever()
8、python中的模块替换
""" 根据URL中不同的路径返回不同的内容 函数进阶版 返回HTML页面 动态起来 wsgiref模块版和jinja2模块 """ import time from wsgiref.simple_server import make_server import jinja2 # 将返回不同的内容部分封装成函数 def index(url): with open("index.html", "r", encoding="utf8") as f: s = f.read() now = str(time.time()) s = s.replace("@@oo@@", now) return bytes(s, encoding="utf8") def home(url): with open("home.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") def xiaoqiang(url): with open("xiaoqiang.html", "r", encoding="utf8") as f: s = f.read() template = jinja2.Template(s) # 生成一个jinja2的Template(模板)对象 data = {"name": "小强", "hobby_list": ["对子哈特", "姑娘", "大汉"]} response = template.render(data) # 本质上是完成了字符串的替换 return bytes(response, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ("/xiaoqiang/", xiaoqiang), ] def run_server(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ]) # 设置HTTP响应的状态码和头信息 url = environ['PATH_INFO'] # 取到用户输入的url func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" return [response, ] if __name__ == '__main__': httpd = make_server('127.0.0.1', 8090, run_server) print("Serving HTTP on port 8090...") httpd.serve_forever()
9、从数据库中返回内容
""" 根据URL中不同的路径返回不同的内容 函数进阶版 返回HTML页面 动态起来 wsgiref模块版和jinja2模块和pymysql """ import time from wsgiref.simple_server import make_server import jinja2 import pymysql # 将返回不同的内容部分封装成函数 def index(url): with open("index.html", "r", encoding="utf8") as f: s = f.read() now = str(time.time()) s = s.replace("@@oo@@", now) return bytes(s, encoding="utf8") def home(url): with open("home.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") def xiaoqiang(url): with open("xiaoqiang.html", "r", encoding="utf8") as f: s = f.read() template = jinja2.Template(s) # 生成一个jinja2的Template(模板)对象 # data = {"name": "小强", "hobby_list": ["对子哈特", "姑娘", "大汉"]} conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="userinfo", charset="utf8") cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) cursor.execute("SELECT name, hobby FROM user") user = cursor.fetchone() cursor.close() conn.close() print("user:", user) print("*" * 120) hobby_list = user["hobby"].split() user["hobby_list"] = hobby_list print(user) print("-" * 120) response = template.render(user) # 本质上是完成了字符串的替换 return bytes(response, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/index/", index), ("/home/", home), ("/xiaoqiang/", xiaoqiang), ] def run_server(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html;charset=utf8'), ]) # 设置HTTP响应的状态码和头信息 url = environ['PATH_INFO'] # 取到用户输入的url func = None for i in list1: if i[0] == url: func = i[1] break if func: response = func(url) else: response = b"404 not found!" return [response, ] if __name__ == '__main__': httpd = make_server('127.0.0.1', 8090, run_server) print("Serving HTTP on port 8090...") httpd.serve_forever()