<Django>socket简单实现django简化版

it2022-05-05  121

服务端(自己实现django)

''' django简化版:socket服务端 a.收发浏览器信息----wsgiref.py b.根据用户访问的不同路径执行不同函数 c.从html读取出内容,并完成字符串的替换(动态网页) 按照功能划分web框架 1.框架自带a,b,c------tornado框架 2.框架自带b,c,使用第三方的a-------django框架 3.框架自带b,使用第三方a和c--------flask 按照维度划分 1.django----大而全,网站能用的都有 2.其他------fLask轻量级 ''' import socket # 生成socket实例对象 sk = socket.socket() # 绑定ip和端口 sk.bind(("127.0.0.1", 8000)) # 监听 sk.listen() # 处理xiaohei的函数 def xiaohei(url): with open("xiaohei.html",'r',encoding='utf-8') as f: ret = f.read() import time # 动态网页本质都是字符串的替换--发生在服务端 ret = ret.replace("@@&&@@",str(time.ctime())) return bytes(ret,encoding='utf-8') # ret = "<h1>hello {} xiaohei<h1>".format(url) # return bytes(ret,encoding='utf-8') def xiaobai(url): with open("xiaobai.html",'rb') as f: ret = f.read() return ret # ret = "<h2>hello {} xiaobai<h2>".format(url) # return bytes(ret,encoding='utf-8') def f404(url): ret = "找不到{}这个url".format(url) return bytes(ret,encoding='utf-8') # urls.py url_func = [ ("/xiaohei/",xiaohei), ("/xiaobai/",xiaobai), ] # 一直等待链接 while 1: # 获取客户端的链接和地址 conn, addr = sk.accept() # 没有用的变量通常用下划线表示 # conn,_ = sk.accept() # 接收消息 data = conn.recv(8989) # 把收到的数据转成字符串类型 data_str = str(data, encoding="utf-8") # bytes("str", enconding="utf-8") print(data_str) # 用\r\n去切割上面的字符串-----切割出路径 l1 = data_str.split("\r\n") # 按照空格切割上面的字符串-----切割出url l2 = l1[0].split() url = l2[1] print(url) # 回复消息消息必须包含四部分:即响应格式(协议版本,状态码,状态描述,回车符) conn.send(b'http/1.1 200 OK\r\ncontent-type:text/html;charset=utf-8\r\n\r\n') # 空行后面接响应正文,想让浏览器在页面上显示出来的内容都是响应正文 # 根据不同url返回不同内容 # if url == '/xiaohei/': # response = xiaohei(url) # else: # response=b"404 not found" # conn.send(b'<h1>hello world<h1>') # conn.send(response) for i in url_func: if i[0] == url: func = i[1] break else: func = f404 response = func(url) conn.send(response) # 关闭 conn.close() sk.close()

  

Django的wsgiref模块

""" 根据URL中不同的路径返回不同的内容--函数进阶版 返回HTML页面 让网页动态起来 wsgiref模块版 """ import time from wsgiref.simple_server import make_server # 将返回不同的内容部分封装成函数 def xiaohei(url): with open("xiaohei.html", "r", encoding="utf8") as f: s = f.read() now = str(time.time()) s = s.replace("@@&&@@", now) return bytes(s, encoding="utf8") def xiaobai(url): with open("xiaobai.html", "r", encoding="utf8") as f: s = f.read() return bytes(s, encoding="utf8") # 定义一个url和实际要执行的函数的对应关系 list1 = [ ("/xiaohei/", xiaohei), ("/xiaobai/", xiaobai), ] 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', 8080, run_server) print("我在8080等你哦...") httpd.serve_forever()

xiaohei.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>xiaohei</title> </head> <body> <h1>返回html文件</h1> <p>随便写几句</p> <p>随便写几句</p> <p>随便写几句</p> <a href="https://www.douyu.com/directory/all"><h1>斗鱼</h1></a> <p>@@&&@@</p> </body> </html>

xiaohei.html

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>xiaobai</title> </head> <body> <h1>用来测试的第二个页面</h1> <h1>用来测试的第二个页面</h1> <h1>用来测试的第二个页面</h1> <h1>用来测试的第二个页面</h1> </body> </html>

  

转载于:https://www.cnblogs.com/shuimohei/p/10704202.html


最新回复(0)