#-*- coding:utf-8 -*-

import sys, os
from http.server import BaseHTTPRequestHandler, HTTPServer #----------------------------------------------------------------------------------
class ServerException(Exception):
'''服务器内部错误'''
pass #----------------------------------------------------------------------------------
class base_case(object):
"""条件处理基类""" #从地址中读取文件送给send_content()返回请求
def handle_file(self,hander,full_path):
try:
with open(full_path, 'rb') as reader:
content = reader.read()
hander.send_content(content)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(full_path, msg)
hander.handle_error(msg) #将两个路径组合后返回为首页路径
def index_path(self, handler):
return os.path.join(handler.full_path, 'index.html') #
def test(self, handler):
assert False,'Not implemented' #
def act(self, handler):
assert False,'Not implemented' #----------------------------------------------------------------------------------
class case_no_file(base_case):
'''文件或目录不存在''' def test(self, handler):
return not os.path.exists(handler.full_path)
#如果path存在,返回True;如果path不存在,返回False def act(self, handler):
raise ServerException("'{0}' not found".format(handler.path)) #----------------------------------------------------------------------------------
class case_cgi_file(base_case):
'''可执行脚本''' def run_cgi(self, handler):
data = subprocess.check_output(["python3", handler.full_path],shell=False)
handler.send_content(data) def test(self, handler):
return os.path.isfile(handler.full_path) and \
handler.full_path.endswith('.py') def act(self, handler):
self.run_cgi(handler) #----------------------------------------------------------------------------------
class case_existing_file(base_case):
'''该路径是文件''' def test(self, handler):
return os.path.isfile(handler.full_path)
#如果path是一个存在的文件,返回True。否则返回False def act(self, handler):
self.handle_file(handler,handler.full_path) #----------------------------------------------------------------------------------
class case_directory_index_file(base_case):
'''在跟路径下返回主页文件''' #判断目标路径是否是目录&&目录下是否有index.html
def test(self, handler):
return os.path.isdir(handler.full_path) and \
os.path.isfile(self.index_path(handler))
#isdir函数判断某一路径是否为目录 #响应index.html的内容
def act(self, handler):
self.handle_file(handler,self.index_path(handler)) #----------------------------------------------------------------------------------
class case_always_fail(object):
'''所有情况都不符合时的默认处理类''' def test(self, handler):
return True def act(self, handler):
raise ServerException("Unknown object '{0}'".format(handler.path)) #一、创建一个请求处理类
#1.模块的 BaseHTTPRequestHandler类 会帮我们处理对请求的解析
#2.并通过确定请求的方法来调用其对应的函数(方法Get对应do_get方法)
#----------------------------------------------------------------------------------
#RequestHandler 继承了 BaseHTTPRequestHandler 并重写了 do_GET 方法
class RequestHandler(BaseHTTPRequestHandler):
'''
请求路径合法则返回相应处理
否则返回错误页面
'''
Cases = [case_no_file(),
case_cgi_file(),
case_existing_file(),
case_directory_index_file(),
case_always_fail()] # 错误页面模板
Error_Page = """\
<html>
<body>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
""" def do_GET(self):
try:
#得到完整的请求路径
#os.getcwd()返回当前进程的工作目录
#BaseHTTPRequestHandler类使请求的相对路径保存在self.path
self.full_path = os.getcwd() + self.path # 遍历所有的情况并处理
for case in self.Cases:
if case.test(self):
case.act(self)
break # 处理异常
except Exception as msg:
self.handle_error(msg) #错误处理函数
def handle_error(self, msg):
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content.encode("utf-8"),404) #content内容被编码为二进制 # 发送数据到客户端
def send_content(self,content, status=200):
self.send_response(status)
self.send_header("Content-Type", "text/html") #Content-Type 告诉客户端要以处理html文件的方式处理返回的内容
self.send_header("Content-Length", str(len(content)))
self.end_headers() #end_headers 方法会插入一个空白行
self.wfile.write(content) #主函数
if __name__ == '__main__':
serverAddress = ('', 8080)
#二、实例化一个服务器类,传入服务器的地址和请求处理程序类
server = HTTPServer(serverAddress, RequestHandler)
#三、调用handle_request()(一般是调用其他事件循环或者使用select())或serve_forever()
server.serve_forever()

Python练习 | WebServer的更多相关文章

  1. 【webserver】使用python实现webserver,支持上传下载文件

    #!/usr/bin/env python """Simple HTTP Server With Upload. This module builds on BaseHT ...

  2. python 打印Linux中文编码字符

    2018-10-12 12:02:15 星期五 python -c "print '\346\234\215\345\212\241\345\231\250\346\217\220\344\ ...

  3. python uwsgi 部署以及优化

    这篇文章其实两个月之前就应该面世了,但是最近琐事.烦心事太多就一直懒得动笔,拖到现在才写 一.uwsgi.wsgi.fastcgi区别和联系 参见之前的文章 http://www.cnblogs.co ...

  4. python实现网页登录时的rsa加密流程

    对某些网站的登录包进行抓包时发现,客户端对用户名进行了加密,然后传给服务器进行校验. 使用chrome调试功能断点调试,发现网站用javascript对用户名做了rsa加密. 为了实现网站的自动登录, ...

  5. 20145319 《网络渗透》web基础

    20145319 <网络渗透>web基础 实验要求 掌握网页编程的基本知识 html语法 javascript php 前端,后台,数据库之间如何建立连接 掌握数据库的使用 mysql的启 ...

  6. WSGI、flup、fastcgi、web.py、uwsgi

    ==================        网上别人的理解 =================== http://www.douban.com/note/13508388/ 1.Apache/ ...

  7. python开启简单webserver

    python开启简单webserver linux下面使用 python -m SimpleHTTPServer 8000 windows下面使用上面的命令会报错,Python.Exe: No Mod ...

  8. python中一个简单的webserver

     python中一个简单的webserver 2013-02-24 15:37:49 分类: Python/Ruby 支持多线程的webserver   1 2 3 4 5 6 7 8 9 10 11 ...

  9. [Top-Down Approach] Assignment 1: WebServer [Python]

    Today I complete Socket Programming Assignment 1 Web Server Here is the code: #!/usr/bin/python2.7 # ...

随机推荐

  1. 'for' loop initial declarations are only allo

    linux系统下的c编程与windows有所不同,如果你在用gcc编译代码的时候提示‘for’ loop initial declarations are only allowed in C99 mo ...

  2. SSH整合案例

    1.Hibernate框架 Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernat ...

  3. UML的常用关系及其符号表示

    原创 UML的常用关系及其符号表示 一.实现关系 通常是一个类实现一个接口 符号表示: 二.泛化关系 通常是一个类继承另外一个类 符号表示: 三.依赖关系 通常是一个类里面的方法的参数类型是另一个类 ...

  4. vs2008编译opencv,不能copy CMakeVSMacros2.vsmacros

    由于学习opencv,要查看源码文件,所以要先对opencv进行编译,可悲的是出错了 “不能copy   CMakeVSMacros2.vsmacros” 通过上网查找资料,之所以出现这种情况,是因为 ...

  5. 在Android模拟器里安装apk

    [原文]http://Android.tgbus.com/android/tutorial/201104/349532.shtml 1.运行SDK Manager,选择模拟器,并运行模拟器. 2.将需 ...

  6. [转载].NET Web开发技术(补充)

    大家在工作应该养成善于总结的习惯,总结你所学习.使用的技术,总结你所工作事项的比较好的地方,善于总结不断的沉淀优化自己.适时停下来总结下过去走过的路,才能让我们的未来走的更坚定.文章转自JamesLi ...

  7. 利用Senparc.Weixin SDK 实现微信用户的授权,并获取信息

    前一段时间在学校做过一个项目,就是利用的Senparc.Weixin SDK 做的,于是翻看以前代码,虽然有注释,但是还是看的迷迷糊糊的,干脆就单步执行一遍看看是怎么实现的,然后就重新写了个简易的授权 ...

  8. 小程序:怎么在两层列表循环(wx:for)的时候判断是否为最后一个元素

    问题说明: 如下图所示,在箭头所指的最后一个选项的底线与底部操作栏的上边线重叠,需要清除掉最后一个元素的底线: 想到的解决方案:  通过判断是否为最后一个元素,然后通过条件渲染(wx:if)动态添加对 ...

  9. numpy 数组运算

    数组的减法:不同维数

  10. 三、linux基础命令

    1.已知/tmp 目录下已经存放了test.txt文件,如何执行命令才能把/mnt/test.txt拷贝到/tmp 下覆盖掉/tmp/test.txt,而让linux系统不提示是否覆盖 答:之所以会出 ...