简易Web服务器

迈不过友情╰ 2022-06-08 10:00 336阅读 0赞

基础知识点

首先简单认识TCP/IP协议
请访问http://www.cnblogs.com/roverliang/p/5176456.html

TCP/IP协议族中有一个重要的概念是分层,TCP/IP协议按照层次分为以下四层
这里写图片描述

TCP/IP通信数据流
这里写图片描述

使用HTTP协议进行数据传输的过程

这里写图片描述

HTTP请求报文
这里写图片描述

HTTP响应报文
这里写图片描述

TCP协议三次握手过程分析
请访问 http://blog.csdn.net/wwwdc1012/article/details/77857265

Python CGI编程 : http://www.runoob.com/python/python-cgi.html
CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端HTML页面的接口。

Python BaseHTTPServer 模块解析
http://blog.csdn.net/xhw88398569/article/details/49179967


下面为一个简单的Web服务器

该服务器工作流程
(1) 定义多个可能遇到的多种情况及对应的处理方法:
是否是 cgi 请求 -> 文件是否存在 -> 目录下是否存在index.html -> 目录下是否不存在index.html -> 其他情况

  1. Cases = [case_no_file(),
  2. case_cgi_file(),
  3. case_existing_file(),
  4. case_directory_index_file(),
  5. case_directory_no_index_file(),
  6. case_always_fail()]

(2) 逐个检查匹配到哪种情况,并处理

  1. for case in self.Cases:
  2. if case.test(self):
  3. case.act(self)
  4. break
  5. import sys, os, BaseHTTPServer
  6. #-----------------------------------------------------------
  7. # 服务器异常类
  8. class ServerException(Exception):
  9. '''For internal error reporting.'''
  10. pass
  11. #-----------------------------------------------------------
  12. class base_case(object):
  13. '''Parent for case handlers.'''
  14. def handle_file(self, handler, full_path):
  15. """处理文件,例如读取 index.html 文件,然后输出到响应报文中 """
  16. try:
  17. with open(full_path, 'rb') as reader:
  18. content = reader.read()
  19. handler.send_content(content)
  20. except IOError as msg:
  21. msg = "'{0}' cannot be read: {1}".format(full_path, msg)
  22. handler.handle_error(msg) # 异常信息
  23. def index_path(self, handler):
  24. """合成index.html的全路径 """
  25. return os.path.join(handler.full_path, 'index.html')
  26. def test(self, handler):
  27. assert False, 'Not implemented.'
  28. def act(self, handler):
  29. assert False, 'Not implemented.'
  30. #-----------------------------------------------------------
  31. class case_no_file(base_case):
  32. '''File or directory does not exist. 给一个路径名称,文件或目录都不存在的情况 '''
  33. def test(self, handler):
  34. # 测试路径是否存在
  35. return not os.path.exists(handler.full_path)
  36. def act(self, handler):
  37. # 不存在时执行的动作,返回服务器异常
  38. raise ServerException("'{0}' not found".format(handler.path))
  39. #-----------------------------------------------------------
  40. class case_cgi_file(base_case):
  41. '''Something runnable. cgi,当服务器上的脚本存在时则执行脚本,返回执行结果 '''
  42. def run_cgi(self, handler): # 执行脚本
  43. cmd = "python " + handler.full_path
  44. child_stdin, child_stdout = os.popen2(cmd)
  45. child_stdin.close()
  46. data = child_stdout.read()
  47. child_stdout.close()
  48. handler.send_content(data)
  49. def test(self, handler): # 测试脚本是否存在
  50. return os.path.isfile(handler.full_path) and \
  51. handler.full_path.endswith('.py')
  52. def act(self, handler):
  53. self.run_cgi(handler)
  54. #-----------------------------------------------------------
  55. class case_existing_file(base_case):
  56. '''File exists. 给一个文件名,当文件存在时,处理 '''
  57. def test(self, handler):
  58. return os.path.isfile(handler.full_path)
  59. def act(self, handler):
  60. self.handle_file(handler, handler.full_path)
  61. #-----------------------------------------------------------
  62. class case_directory_index_file(base_case):
  63. '''Serve index.html page for a directory. 给一个目录名,当该目录下存在index.html时,处理并返回该index.html '''
  64. def test(self, handler):
  65. return os.path.isdir(handler.full_path) and \
  66. os.path.isfile(self.index_path(handler))
  67. def act(self, handler):
  68. self.handle_file(handler, self.index_path(handler))
  69. #-----------------------------------------------------------
  70. class case_directory_no_index_file(base_case):
  71. '''Serve listing for a directory without an index.html page. 目录下不存在index.html时,列出该目录下的所有文件,并渲染到简单的模板中 '''
  72. # How to display a directory listing.
  73. Listing_Page = '''\ <html> <body> <ul> {0} </ul> </body> </html> '''
  74. def list_dir(self, handler, full_path):
  75. try:
  76. entries = os.listdir(full_path)
  77. bullets = ['<li>{0}</li>'.format(e) for e in entries if not e.startswith('.')]
  78. page = self.Listing_Page.format('\n'.join(bullets))
  79. handler.send_content(page)
  80. except OSError as msg:
  81. msg = "'{0}' cannot be listed: {1}".format(self.path, msg)
  82. handler.handle_error(msg)
  83. def test(self, handler):
  84. return os.path.isdir(handler.full_path) and \
  85. not os.path.isfile(self.index_path(handler))
  86. def act(self, handler):
  87. self.list_dir(handler, handler.full_path)
  88. #------------------------------------------------------
  89. class case_always_fail(base_case):
  90. '''Base case if nothing else worked. 当之前所有的情况都失败时,执行这个 '''
  91. def test(self, handler):
  92. return True
  93. def act(self, handler):
  94. raise ServerException("Unknown object '{0}'".format(handler.path))
  95. #------------------------------------------------------
  96. class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  97. ''' If the requested path maps to a file, that file is served. If anything goes wrong, an error page is constructed. '''
  98. Cases = [case_no_file(),
  99. case_cgi_file(),
  100. case_existing_file(),
  101. case_directory_index_file(),
  102. case_directory_no_index_file(),
  103. case_always_fail()]
  104. # How to display an error. 显示错误信息的模板
  105. Error_Page = """\ <html> <body> <h1>Error accessing {path}</h1> <p>{msg}</p> </body> </html> """
  106. # Classify and handle request. 处理GET请求
  107. def do_GET(self):
  108. try:
  109. # Figure out what exactly is being requested.
  110. self.full_path = os.getcwd() + self.path
  111. # Figure out how to handle it.
  112. for case in self.Cases:
  113. if case.test(self):
  114. case.act(self)
  115. break
  116. # Handle errors.
  117. except Exception as msg:
  118. self.handle_error(msg)
  119. # Handle unknown objects.
  120. def handle_error(self, msg):
  121. content = self.Error_Page.format(path=self.path, msg=msg)
  122. self.send_content(content, 404)
  123. # Send actual content. 设置响应报文
  124. def send_content(self, content, status=200):
  125. self.send_response(status)
  126. self.send_header("Content-type", "text/html")
  127. self.send_header("Content-Length", str(len(content)))
  128. self.end_headers()
  129. self.wfile.write(content)
  130. #-------------------------------------------------------------------------------
  131. if __name__ == '__main__':
  132. serverAddress = ('', 8080)
  133. server = BaseHTTPServer.HTTPServer(serverAddress, RequestHandler)
  134. server.serve_forever()

参考:500 lines or less 中文翻译计划 《简易Web服务器》
https://github.com/HT524/500LineorLess_CN/blob/master/%E7%AE%80%E6%98%93web%E6%9C%8D%E5%8A%A1%E5%99%A8%20A%20simple%20web%20server/%E7%AE%80%E6%98%93web%E6%9C%8D%E5%8A%A1%E5%99%A8.md

发表评论

表情:
评论列表 (有 0 条评论,336人围观)

还没有评论,来说两句吧...

相关阅读