前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >面向对象封装的web服务器

面向对象封装的web服务器

作者头像
汪凡
发布2018-05-29 15:18:26
6510
发布2018-05-29 15:18:26
举报
文章被收录于专栏:python成长之路python成长之路
代码语言:javascript
复制
import socket
import re
import os
import sys

# 由于前面太繁琐,可以用类封装一下,也可以分几个模块
class HttpServer(object):

    def __init__(self,port):
        # 1、服务器创建负责监听的socket
        self.socket_watch = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # 2、设置地址重用
        self.socket_watch.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        # 3、绑定监听的端口
        self.socket_watch.bind(('', port))
        # 4、设置监听队列
        self.socket_watch.listen(128)

    def handle_client(self,socket_con):
        """
         接收来自客户端的请求,并接收请求报文,解析,返回
        """
        # 1、服务器接收客户端的请求报文
        request = socket_con.recv(4096).decode()

        # 2、截取请求报文,获取请求行
        request_lines = request.split("\r\n")
        # 3、获取请求行
        request_line = request_lines[0]
        # GET /a/ab/c.html HTTP/1.1
        # 通过正则表达式 匹配出请求行中请求资源路径
        res = re.match(r"\w+\s+(\S+)",request_line)
        # 获取资源路径
        path = res.group(1)
        # 将资源路径和我的web文件夹的绝对路径拼接(自己填写)
        path ="# 本地绝对路径" + path
        # 在判断是文件还是文件夹之前,首先要判断你这个路径在服务器中是否存在
        if not os.path.exists(path):
            response_line = 'HTTP/1.1 404 Not Found\r\n'
            response_head = 'Server:skylark 2.0\r\n'
            response_head += 'Content-type:text/html;charset=utf-8\r\n'
            response_body = '你请求'+ path +'不存在'
            response = response_line + response_head + '\r\n' +response_body
            socket_con.send(response.encode())
            socket_con.close()
            return
        else:
            # 判断用户请求的是文件还是文件夹
             if os.path.isfile(path):
                 # 如果文件存在 读取页面数据,然后返回
                response_line = "HTTP/1.1 200 OK\r\n"
                response_head = "Server:skylark 2.0\r\n"
                # 注意请求图片需要使用"rb"的方式进行读取
                file = open(path,"rb")
                # response_body 是二进制所以不用再次编码
                response_body = file.read()
                response = response_line.encode() + response_head.encode() +"\r\n".encode() +response_body
                socket_con.send(response)
                socket_con.close()
                return
             else:
                if path.endswith("/"):
                    # 例如 www.baidu.com/images
                    # 用户请求的文件夹
                    # 1、判断该文件夹下是否有默认的文件,如果有,则返回,如果没有
                    # index.html default.html
                    default_document = False
                    # 如果允许你访问我目录下的默认文档
                    if default_document:
                        # 判断用户访问的文件夹下是否有index.html 或者 default.html
                        if os.path.exists(path + '/index.html'):
                            response_line = 'HTTP/1.1 200 OK\r\n'
                            response_head = 'Server:skylark 2.0\r\n'
                            file = open(path+'/index.html', 'rb')
                            response_body = file.read()
                            response = response_line.encode() + response_head.encode() +'\r\n'.encode()+response_body
                            socket_con.send(response)
                            socket_con.close()
                            return
                        elif os.path.exists(path + '/default.html'):
                            response_line = 'HTTP/1.1 200 OK\r\n'
                            response_head = 'Server:skylark 2.0\r\n'
                            file = open(path + '/default.html', 'rb')
                            response_body = file.read()
                            response = response_line.encode() + response_head.encode() + '\r\n'.encode() + response_body
                            socket_con.send(response)
                            socket_con.close()
                            return
                        else:
                            # 访问的目录下,既没有index.html 也没有default.html
                            response_line = 'HTTP/1.1 404 Not Found\r\n'
                            response_head = 'Server:skylark 2.0\r\n'
                            response_head += 'Content-Type:text/html;charset=utf-8\r\n'
                            response_body = 'index.html 或者 default.html 不存在'
                            response = response_line +response_head +'\r\n' +response_body
                            socket_con.send(response.encode())
                            socket_con.close()
                        # 2、判断服务器是否开启了目录浏览
                    else:
                        # 判断你是否开启了目录浏览
                        dir_browsing = True
                        if dir_browsing:
                            # 把用户请求的文件夹中所有的文件和文件夹以目录的形式返回到页面中
                            # 获取用户请求的文件夹
                            list_names = os.listdir(path)
                            response_line = 'HTTP/1.1 200 OK\r\n'
                            response_head = 'Server:skylark 2.0\r\n'
                            # 动态的拼接页面,将目录中的文件或者文件夹的名称以HTML页面的方式返回给浏览器
                            response_body = '<html><head><body><ul>'
                            for item in  list_names:
                                response_body +="<li><a href = '#'>"+item+"</a></li>"
                            response_body+='</ul></body></head></html>'
                            response =response_line + response_head +'\r\n' +response_body
                            socket_con.send(response.encode())
                            socket_con.close()
                            return

                else:
                    # 用户请求的路径没有斜线
                    # 重定向到+斜线的目录下
                    response_line = 'HTTP/1.1 302 Found\r\n'
                    response_head = 'Server:skylark 2.0\r\n'
                    response_body = 'redirect'+ path +'/'
                    response = response_line +response_head +'\r\n' +response_body
                    socket_con.send(response.encode())
                    socket_con.close()

    def run_server(self):
        # 5、通过循环,不停的接收来自客户端的连接请求
        while True:
            socket_con, con_adds = self.socket_watch.accept()
            # 注意将con_adds转成字符串
            print('客户端:%s连接成功!!!' % str(con_adds))
            # 接收来自客户端的请求,并接收请求报文,解析,返回
            self.handle_client(socket_con)

def main():
    # sys.argv方法的用法如下:
    # 在终端输入 python3 面向对象封装的web服务器.py 8888
    # 在使用解释器执行任意py文件的时候,可以传入不止一个参数,会以字符串的形式用列表保存起来
    # 但是列表的第一个参数[0]位是它自己。所以传入的参数是从[1]第二位开始的
    # 所以在上面输入8888以后,调取这个列表的[1]下标就会传入这个8888作为进到下面的代码
    # 再转换一下类型为int就相当于用户指定端口了
    port = int(sys.argv[1])
    http_server = HttpServer(port)
    http_server.run_server()


if __name__ == '__main__':
    main()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档