2.WSGI服务
# -*- coding:utf-8 -*- from wsgiref.simple_server import make_server def run_server(environ, start_response): print(environ) print(start_response) start_response("200 OK",[('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 >欢迎来到我的博客</h2>', encoding="utf-8")] s = make_server('localhost', 8000, run_server) s.serve_forever()
实现多个url
# -*- coding:utf-8 -*- from wsgiref.simple_server import make_server # 1.路由器的分发器,负责把url 匹配到对应的函数 # 2.开发好对应的业务函数 # 3.一个请求来了之后,先走路由分发器,如果找到对应的function,就执行,如果没找到返回 404 def book(environ, start_response): print("book page") start_response("200 OK", [('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 >book page</h2>', encoding="utf-8")] def cloth(environ, start_response): print("cloth page") start_response("200 OK", [('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 style="color:red">cloth page</h2>', encoding="utf-8")] def url_dispacher(): urls = { '/book': book, '/cloth': cloth, } return urls def run_server(environ, start_response): print(environ) print(start_response) url_list = url_dispacher() # 拿到所有的url request_url = environ.get("PATH_INFO") print('request url', request_url) if request_url in url_list: func_data = url_list[request_url](environ, start_response) return func_data # 真正返回给用户数据 else: start_response("404 ", [('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 style="font-size:5px">404,page not found</h2>', encoding="utf-8")] # start_response("200 OK",[('Content-Type', 'text/html;charset=utf-8')]) # return [bytes('<h2 >欢迎来到我的博客</h2>', encoding="utf-8")] s = make_server('localhost', 8000, run_server) s.serve_forever()
图片的显示
图片目录
# -*- coding:utf-8 -*- from wsgiref.simple_server import make_server import os import re # 1.路由器的分发器,负责把url 匹配到对应的函数 # 2.开发好对应的业务函数 # 3.一个请求来了之后,先走路由分发器,如果找到对应的function,就执行,如果没找到返回 404 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) def book(environ, start_response): print("book page") start_response("200 OK", [('Content-Type', 'text/html;charset=utf-8')]) data = """ <h1>程序员专区</h1> <img src = '/static/imgs/6.gif'> <p>小小程序员</p> """ return [bytes(data, encoding="utf-8")] def cloth(environ, start_response): print("cloth page") start_response("200 OK", [('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 style="color:red">cloth page</h2>', encoding="utf-8")] def url_dispacher(): urls = { '/book': book, '/cloth': cloth, } return urls def img_handler(request_url): """ :param request_url: /static/imgs/6.gif :return: """ img_path = re.sub('/static', '/static_data',request_url) img_abs_path = "%s%s" %(BASE_DIR,img_path) print("Base", BASE_DIR) print(img_abs_path) if os.path.isfile(img_abs_path): print('-------- img exist .....') f = open(img_abs_path, 'rb') data = f.read() # 读取图片 return [data, 0] # 0代表没错, 1代表 有错 return [None, 1] def run_server(environ, start_response): print(environ) print(start_response) url_list = url_dispacher() # 拿到所有的url request_url = environ.get("PATH_INFO") print('request url', request_url) if request_url in url_list: func_data = url_list[request_url](environ, start_response) return func_data # 真正返回给用户数据 elif request_url.startswith('/static/'): img_data, img_status = img_handler(request_url) if img_status == 0: start_response("200 OK ", [('Content-Type', 'text/html;charset=utf-8')]) return [img_data,] else: start_response("404 ", [('Content-Type', 'text/html;charset=utf-8')]) return [bytes('<h2 style="font-size:5px">404,page not found</h2>', encoding="utf-8")] # start_response("200 OK",[('Content-Type', 'text/html;charset=utf-8')]) # return [bytes('<h2 >欢迎来到我的博客</h2>', encoding="utf-8")] s = make_server('localhost', 8000, run_server) s.serve_forever()
socket服务
# -*- coding:utf-8 -*- import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind(('localhost', 8000)) sock.listen(5) while True: # 等待浏览器访问 conn, addr = sock.accept() # 接收浏览器发来的请求内容 data = conn.recv(1024) print(data) # 给浏览器返回内容 conn.send(b"HTTP/1.1.200 OK\r\nContent-Type:text/html; charset=utf-8\r\n\r\n") conn.send("<h2 >欢迎来到我的博客</h2>".encode('utf-8')) # 关闭浏览器 conn.close() if __name__ == '__main__': main()