web服务器之mini_frame框架demo

dynamic/mini_frame.py

import re
import pymysql
import urllib.parse
import logging


URL_FUNC_DICT = dict()
"""
URL_FUNC_DICT = {
        "/index.py" : index,
        "/center.py" : center,
        }
"""
# func_list = list()

def route(url):
    def set_func(func):
        # func_list.append(func)
        URL_FUNC_DICT[url] = func
        def call_func(*args, **kwargs):
            return func(*args, **kwargs)
        return call_func
    return set_func


@route(r"/index.html")
def index(ret):
    with open("./templates/index.html") as f:
        content = f.read()
    # my_stock_info = "这里是从mysql中查询出来的数据2..."
    # content =  re.sub(r"\{%content%\}",my_stock_info, content)


    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("select * from info;")
    stock_info = cursor.fetchall()
    cursor.close()
    conn.close()
    tr_template = """<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>
    <input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="%s">
    </td>
    </tr>
    """
    html = ""
    for line_info in stock_info:
        html += tr_template % (line_info[0],line_info[1],line_info[2],line_info[3],line_info[4],line_info[5],line_info[6],line_info[7],line_info[1])
    content =  re.sub(r"\{%content%\}",html, content)
    return content


@route(r"/center.html")
def center(ret):
    with open("./templates/center.html") as f:
        content = f.read()
    # my_stock_info = "这里是从mysql中查询出来的数据..."

    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("select i.code , i.short , i.chg, i.turnover, i.price, i.highs , f.note_info  from info as i inner join focus as f on i.id = f.info_id;")
    stock_info = cursor.fetchall()
    cursor.close()
    conn.close()
    tr_template = """<tr>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>%s</td>
    <td>
        <a type="button" class="btn btn-default btn-xs" href="/update/%s.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
    </td>
    <td>
        <input type="button" value="删除" id="toDel" name="toDel" systemidvaule="%s">
    </td>
    </tr>
    """
    html = ""
    for line_info in stock_info:
        html += tr_template % (line_info[0],line_info[1],line_info[2],line_info[3],line_info[4],line_info[5],line_info[6],line_info[0],line_info[0])
    content =  re.sub(r"\{%content%\}",html, content)
    return content 


@route(r"/add/(\d+).html")
def add_focus(ret):
    # print("add_fouces")
    # 1. 获取股票code
    stock_code = ret.group(1)
    # 2.判断是否已经有这只股票
    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("select * from info where code = %s;", (stock_code, ))
    # 如果没有这个股票代码, 那么就任务是非法请求
    if not  cursor.fetchone():
        cursor.close()
        conn.close()
        return "没有这支股票,..大哥, 我们是创业公司, 请手下留情...."

    #3.判断是否已经关注过
    cursor.execute("""select * from info as i inner join focus as f on i.id=f.info_id where i.code=%s;""", (stock_code , ))
    if cursor.fetchone():
        cursor.close()
        conn.close()
        return "已经关注过这支股票了....不用再点了"
    
    #4.添加关注
    cursor.execute("insert into focus (info_id) select id from info where code = %s;" , (stock_code , ))
    conn.commit()
    cursor.close()
    conn.close()
    return "添加成功"


@route(r"/del/(\d+).html")
def del_focus(ret):
    # 1. 获取股票code
    stock_code = ret.group(1)

    # 2.判断是否已经有这只股票
    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("select * from info where code = %s;", (stock_code, ))
    # 如果没有这个股票代码, 那么就任务是非法请求
    if not  cursor.fetchone():
        cursor.close()
        conn.close()
        return "没有这支股票,..大哥, 我们是创业公司, 请手下留情...."

    #3.判断是否已经关注过
    cursor.execute("""select * from info as i inner join focus as f on i.id=f.info_id where i.code=%s;""", (stock_code , ))
    if not cursor.fetchone():
        cursor.close()
        conn.close()
        return "%s之前就没有关注过这支股票了....请勿取消关注" % stock_code
    
    #4.取消关注
    cursor.execute("delete from focus where info_id =(select id from info where code = %s);" , (stock_code , ))
    conn.commit()
    cursor.close()
    conn.close()
    return "取消成功"

@route(r"/update/(\d+)\.html")
def show_update_page(ret):
    """显示修改的那个页面"""
    # 1.获取股票代码
    stock_code = ret.group(1)
    # 2.打开模板
    with open("./templates/update.html") as f:
        content = f.read()
    # 3. 根据股票信息查询相关信息
    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("select f.note_info from focus as f inner join info as i on i.id=f.info_id where i.code=%s;", (stock_code, ))
    stock_info = cursor.fetchone()
    note_info = stock_info[0] # 获取这支股票的备注信息
    cursor.close()
    conn.close()

    content =  re.sub(r"\{%note_info%\}",note_info, content)
    content =  re.sub(r"\{%code%\}",stock_code, content)

    return content 


@route(r"/update/(\d+)/(.*)\.html")
def save_stock_info(ret):
    print("save_stock_info")
    stock_code = ret.group(1)
    comment = ret.group(2)
    comment = urllib.parse.unquote(comment)
    conn = pymysql.connect(host='localhost',port=3306,user='root',password='mysql',database='stock_db',charset='utf8')
    cursor = conn.cursor()
    cursor.execute("update focus set note_info=%s where info_id = (select id from info where code=%s)", (comment,stock_code))
    conn.commit()
    cursor.close()
    conn.close()
    return "修改成功...."


def application(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html;charset=utf-8')])
    file_name = environ['PATH_INFO']
    """
    if file_name == "/index.py":
        return index()
    elif file_name == "/center.py":
        return center()
     """  
    logging.basicConfig(level=logging.WARNING,filename='./log.txt',filemode='a',format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')  
    logging.info("访问的是:%s" % file_name)
    # return URL_FUNC_DICT[file_name]()    
    for url , func  in URL_FUNC_DICT.items():
        # print("---url = %s " % url)
        ret = re.match(url,file_name)
        if ret:
            return func(ret)
    else:
        return "请求的rul(%s)没有对应的函数..." % file_name            

            


my_web_service_demo.py

import socket 
import re
import multiprocessing
import time
#import dynamic.mini_frame
import sys

class WSGIService(object):


    def __init__(self, port, app, static_path):
        self.tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.tcp_socket.bind(("" , port))
        self.tcp_socket.listen(128)
        self.application = app
        self.static_path = static_path

    
    def service_client(self , new_socket):
        request = new_socket.recv(1024).decode("utf-8")
        request_list = request.splitlines()
        
        ret = re.match(r"[^/]+(/[^ ]*)", request_list[0])
        if ret:
            file_name = ret.group(1)
            if file_name == "/":
                file_name = "/index.html"
            print(file_name)
#        print(request)
        
        # 如果请求的资源不是以.py 结尾的. 那么就认为是静态资源
        if not file_name.endswith(".html"): 
            try:
                f = open(self.static_path+ file_name, "rb")
            except:
                response = "HTTP/1.1 404 NOT FOUND\r\n"
                response += "\r\n"
                response += "---file not found---"
                new_socket.send(response.encode("utf-8"))
            else:
                html_content = f.read()
                f.close()
                response = "HTTP/1.1 200 OK\r\n"
                response += "\r\n"
                new_socket.send(response.encode("utf-8"))
                new_socket.send(html_content)
                
        else:
            # 如果以.py 结尾, 那么就认为是动态资源的请求
            # body = mini_frame.login()
            # body = mini_frame.application(file_name)
            env = dict()
            env['PATH_INFO'] = file_name
            # body = dynamic.mini_frame.application(env ,self.set_response_header)
            body = self.application(env ,self.set_response_header)
            header = "HTTP/1.1 %s\r\n" % self.status
            for temp in self.headers:
                header += "%s:%s\r\n" % (temp[0], temp[1])

            header += "\r\n"
            response = header + body
            new_socket.send(response.encode("utf-8"))

        new_socket.close()

    def set_response_header(self,status,headers):
        self.status = status
        self.headers = [("server", "mini_web v8.8")]
        self.headers += headers

    def run_forever(self):
        while True:
            new_socket , client_addr = self.tcp_socket.accept()
            p = multiprocessing.Process(target = self.service_client , args = (new_socket , ))
            # service_client(new_socket)
            p.start()
            new_socket.close()
        self.tcp_socket.close()



def main():
    if len(sys.argv) ==3:
        try:
            port = int(sys.argv[1]) # 端口号
            frame_app_name = sys.argv[2] # mini_frame:application
        except Exception as ret:
            print("端口号有误....")
            return
    else:
        print("请按照以下方式运行:")
        print("python3 xxx.py 端口号 框架名...")
        return
    




    ret = re.match(r"([^:]+):(.*)",frame_app_name)
    if ret:
        frame_name = ret.group(1) # mini_frame
        app_name = ret.group(2) # application
    else:
        print("请按照以下方式运行:")
        print("python3 xxx.py 端口号 框架名...")
        return

    with open("./web_server.conf") as f:
        conf_info = eval(f.read())

    # 增加目录的上一级
    sys.path.append(conf_info['dynamic_path'])
    frame = __import__(frame_name) # 返回值标记的这个导入的OK
    app = getattr(frame, app_name)# 此时app 只想了dynamic中的application函数
    # print(app)

    wsgi_server = WSGIService(port, app, conf_info['static_path'])
    wsgi_server.run_forever()

if __name__ == "__main__":
    main()



猜你喜欢

转载自blog.csdn.net/weixin_42225318/article/details/80670712