• 4 class类 web服务器


     

    1.换行符

        

     2.pycharm 连接Ubuntu

      1)添加环境变量

     

     

      2)查看ip

     

      3)配置目录

     

      4)上传或者下载

     

    3.面向对象抽象web服务器

      1)版本1:类

    class HttpServer(object):
        """"""
        pass
    
    http_server = HttpServer()

      2)版本2:初始化

    class HttpServer(object):
        """"""
        def __init__(self,port):
            self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.server_socket.bind(("",port ))
            self.server_socket.listen(128)
    
    http_server = HttpServer(8080)

      3)版本3:定义方法

    class HttpServer(object):
        """"""
        def __init__(self,port):
            self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.server_socket.bind(("", port))
            self.server_socket.listen(128)
    
        def start(self):
           pass
    
        def hand_client(self):
            pass
    
    http_server = HttpServer(8080)
    http_server.start()

      4)版本4:调整

    #-*- coding:utf-8 -*-
    import socket
    import re
                                     #空行
    from multiprocessing import Process
    
    
    # 设置静态文件根目录
    HTML_ROOT_DIR = "./html"   #常量全部要大写
    
    class HttpServer(object):
        """"""
        def __init__(self,port):
            self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            self.server_socket.bind(("", port))
    
    
        def start(self):
            self.server_socket.listen(128)
            while True:
                client_socket, client_address = self.server_socket.accept()  # 赋值的时候有空格
                print("[%s,%s]用户连接上了" % client_address)
                hand_client_process = Process(target=self.hand_client, args=(client_socket,))
                # =函数名  不能有空格
                hand_client_process.start()
                client_socket.close()  # 多进程会复制 client_socket
    
        def hand_client(self,client_socket):
            """处理客户端请求"""  # 推荐""""""
            # 获取客户端请求数据
            request_data = client_socket.recv(1024)
            print("request_data:", request_data)
            request_data = request_data.decode("utf-8")
            request_lines = request_data.splitlines()
            print(request_lines)
    
            # 解析请求报文
            for line in request_lines:
                print(line)
            # GET /favicon.ico HTTP/1.1
            request_start_line = request_lines[0]
            # 提取用户请求的文件名
            file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1)
            print(file_name)
    
            if "/" == file_name:  # 常量放在等号的左边
                file_name = "/index.html"
    
            # 打开文件读取内容
            try:
                file = open(HTML_ROOT_DIR + file_name, "rb")
            except IOError:
                response_start_line = "HTTP/1.1 404 Not found 
    "
                response_headers = "Server: My server
    "
                response_body = "the file is not found"
            else:
                file_data = file.read()
                file.close()
    
                # 构造响应数据    # 空一格在加注释
                response_start_line = "HTTP/1.1 200 OK 
    "
                response_headers = "Server: My server
    "
                response_body = file_data.decode("utf-8")
    
            response = response_start_line + response_headers + "
    " + response_body
            print("response:", response)
    
            # 向客户端返回相应数据
            client_socket.send(bytes(response, "utf-8"))
    
            # 关闭客户端连接
            client_socket.close()
    
    def main():
        http_server = HttpServer(8080)
        http_server.start()
    
    if __name__ == "__main__":
        main()

      5)版本5:增加bind方法

    #-*- coding:utf-8 -*-
    import socket
    import re
                                     #空行
    from multiprocessing import Process
    
    
    # 设置静态文件根目录
    HTML_ROOT_DIR = "./html"   #常量全部要大写
    
    class HttpServer(object):
        """"""
        def __init__(self):
            self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    
        def start(self):
            self.server_socket.listen(128)
            while True:
                client_socket, client_address = self.server_socket.accept()  # 赋值的时候有空格
                print("[%s,%s]用户连接上了" % client_address)
                hand_client_process = Process(target=self.hand_client, args=(client_socket,))
                # =函数名  不能有空格
                hand_client_process.start()
                client_socket.close()  # 多进程会复制 client_socket
    
        def hand_client(self,client_socket):
            """处理客户端请求"""  # 推荐""""""
            # 获取客户端请求数据
            request_data = client_socket.recv(1024)
            print("request_data:", request_data)
            request_data = request_data.decode("utf-8")
            request_lines = request_data.splitlines()
            print(request_lines)
    
            # 解析请求报文
            for line in request_lines:
                print(line)
            # GET /favicon.ico HTTP/1.1
            request_start_line = request_lines[0]
            # 提取用户请求的文件名
            file_name = re.match(r"w+ +(/[^ ]*)", request_start_line).group(1)
            print(file_name)
    
            if "/" == file_name:  # 常量放在等号的左边
                file_name = "/index.html"
    
            # 打开文件读取内容
            try:
                file = open(HTML_ROOT_DIR + file_name, "rb")
            except IOError:
                response_start_line = "HTTP/1.1 404 Not found 
    "
                response_headers = "Server: My server
    "
                response_body = "the file is not found"
            else:
                file_data = file.read()
                file.close()
    
                # 构造响应数据    # 空一格在加注释
                response_start_line = "HTTP/1.1 200 OK 
    "
                response_headers = "Server: My server
    "
                response_body = file_data.decode("utf-8")
    
            response = response_start_line + response_headers + "
    " + response_body
            print("response:", response)
    
            # 向客户端返回相应数据
            client_socket.send(bytes(response, "utf-8"))
    
            # 关闭客户端连接
            client_socket.close()
    
        def bind(self,port):
            self.server_socket.bind(("", port))
    
    
    def main():
        http_server = HttpServer()
        http_server.bind(8080)
        http_server.start()
    
    if __name__ == "__main__":
        main()
  • 相关阅读:
    Kubernetes 1.5部署sonarqube
    Kubernetes 1.5集成heapster
    Kubernetes 1.5 配置dashboard
    SQL SERVER中的逻辑读取,物理读取,以及预读的理解
    JS控制显示/隐藏二级菜单
    Css下拉菜单设置
    div包裹页面后多余部分没有显示,也没滚动条 overflow 属性设置
    Sql Ado.net 学习笔记之连接字符串
    Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
    SQL中的字母的大小写转换
  • 原文地址:https://www.cnblogs.com/venicid/p/8030067.html
Copyright © 2020-2023  润新知