zoukankan      html  css  js  c++  java
  • 第二十二节 web服务器里集成了解析动态请求的功能

    import socket
    import multiprocessing
    import re
    
    '''
    WSGI协议
    浏览器,http请求动态资源→web服务器(通过wsgi调用一个默认的application函数(字典, 函数名))
    '''
    class WSGIServer():
    
        def __init__(self):
            self.web_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.web_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.web_socket.bind(('192.168.0.106',8080))
            self.web_socket.listen(128)    
    
        def tcp_serve(self, resp_socket):
            recv_data = resp_socket.recv(1024).decode('utf-8')
            request_lines = recv_data.splitlines()
            file_name = ''
            ret = re.match(r"[^/]+(/[^ ]*)",request_lines[0])
            if ret:
                file_name=ret.group(1)
                print(file_name)
                if file_name == "\":
                    file_name = '\index.html'
    
            path = r'E:\360Downloads\Software\新建文件夹\新建文件夹\tmag_23_Infinity'+file_name
            try:
                f=open(path,'rb')
            except:
                resp_data = 'HTTP/1.1 200 OK
    ' + "
    " + '.....file not find....'
                resp_socket.send(resp_data.encode('utf-8'))
            else:
                html_content = f.read()
                f.close()
                resp_data = 'HTTP/1.1 200 OK
    ' + "
    "
                resp_socket.send(resp_data.encode('utf-8'))
                resp_socket.send(html_content)
            resp_socket.close()
    
        def run_forver(self):
            while True:    
                resp_socket, resp_addr = self.web_socket.accept()
                p = multiprocessing.Process(target=self.tcp_serve, args=(resp_socket,))
                p.start()
                # 同乌班图一样也需要关闭套接字,跟文件描述符有关,不然页面一直刷新不出来
                resp_socket.close()
            # self.web_socket.close()
    
    def main():
        wsgi_server = WSGIServer()
        wsgi_server.run_forver()
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    009——数组(九) each list array_map array_walk array_walk_recursive
    008——数组(八)删除添加数组 得到数组键名键值
    laravel怎么获取到public路径
    laravel中composer镜像服务的方式
    logback的使用和logback.xml详解
    log4j-over-slf4j工作原理详解
    java
    【Log】logback指定配置文件(二)
    logback 中文手册
    logback logback.xml常用配置详解(三) <filter>
  • 原文地址:https://www.cnblogs.com/kogmaw/p/12602528.html
Copyright © 2011-2022 走看看