#-*- coding:utf-8 -*-
#author: lichmama
#email: nextgodhand@163.com
#filename: httpd.py
import io
import os
import sys
import urllib
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler class MyRequestHandler(SimpleHTTPRequestHandler):
protocol_version = "HTTP/1.1"
server_version = "PSHS/0.1"
sys_version = "Python/2.7.x"
target = "D:/web" def do_GET(self):
if self.path == "/" or self.path == "/index":
content = open("signin.html", "rb").read()
self.send_head(content)
else:
path = self.translate_path(self.path)
if os.path.exists(path):
extn = os.path.splitext(path)[1].lower()
content = open(path, "rb").read()
self.send_head(content, type=self.extensions_map[extn])
else:
content = open("404.html", "rb").read()
self.send_head(content, code=404)
self.send_content(content) def do_POST(self):
if self.path == "/signin":
data = self.rfile.read(int(self.headers["content-length"]))
data = urllib.unquote(data)
data = self.parse_data(data)
try:
uid = data["uid"]
if uid != "":
content = open("success.html", "rb").read()
content = content.replace("$uid", uid)
self.send_head(content)
#do-something-in-backend
if not os.path.exists(self.target + "/" + uid):
os.mkdir(self.target + "/" + uid)
else:
content = "400, bad request."
self.send_head(content, code=400)
except KeyError:
content = "400, bad request."
self.send_head(content, code=400)
else:
content="403, forbiden."
self.send_head(content, code=403)
self.send_content(content) def parse_data(self, data):
ranges = {}
for item in data.split("&"):
k, v = item.split("=")
ranges[k] = v
return ranges def send_head(self, content, code=200, type="text/html"):
self.send_response(code)
self.send_header("Content-Type", type)
self.send_header("Content-Length", str(len(content)))
self.end_headers() def send_content(self, content):
f = io.BytesIO()
f.write(content)
f.seek(0)
self.copyfile(f, self.wfile)
f.close() if __name__ == "__main__":
if len(sys.argv) == 2:
#set the target where to mkdir, and default "D:/web"
MyRequestHandler.target = sys.argv[1]
try:
server = HTTPServer(("", 8080), MyRequestHandler)
print "pythonic-simple-http-server started, serving at http://localhost:8080"
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()

python webserver, based on SimpleHTTPServer的更多相关文章

  1. python标准库学习-SimpleHTTPServer

    这是一个专题 记录学习python标准库的笔记及心得 简单http服务 SimpleHTTPServer 使用 python -m SimpleHTTPServer 默认启动8000端口 源码: &q ...

  2. 通过Python自带模块SimpleHTTPServer快速共享服务的配置文件

    简介 SimpleHTTPServer是Python 2自带的一个模块,是Python的Web服务器,简单小巧,快速启动. 它在Python 3已经合并到http.server模块中. SimpleH ...

  3. Python WebServer with MSSql

    今天尝试了一下在windows上用python来写web服务 我的环境是 win7(64位)+ python(2.7.11) 第一步需要安装pymssql 第二步需要安装tornado(web服务靠他 ...

  4. python webserver客户端

    1.库 suds库,只能做webserver客户端,轻量化,使用方便.安装使用pip. 2.使用 如有webserver情况如下: url:http://10.110.35.41:8980/wsser ...

  5. 【Python 开发】第三篇:python 实用小工具

    一.快速启动一个web下载服务器 官方文档:https://docs.python.org/2/library/simplehttpserver.html 1)web服务器:使用SimpleHTTPS ...

  6. 【转】Python资源 转自 dylanninin.com

    http://dylanninin.com/blog/2013/11/23/python_resource.html Python是在工作期间零零碎碎学习起来的.当时正值部门申购图书,鉴于Python ...

  7. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  8. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  9. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

随机推荐

  1. iOS Regex匹配关键字并修改颜色

    引入第三方框架RegexKitLite /** * 根据传入的文字返回一个符合规则的富文本 * * @param title 匹配的文字 * * @return 创建的富文本 */ -(NSAttri ...

  2. 你会python不?当你听到这个问题要谨慎回答!!!

    问:你会python不? 答:python啊,略微有点小研究,虽然不精通,但是写写网络小爬虫,搜集搜集网络资源,学习视频什么的,还是手到擒来的...(for循环一小时中) 旁白:然而你没有明白人家的真 ...

  3. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  4. [转载]OpenStack OVS GRE/VXLAN网络

      学习或者使用OpenStack普遍有这样的现象:50%的时间花费在了网络部分:30%的时间花费在了存储方面:20%的时间花费在了计算方面.OpenStack网络是不得不逾越的鸿沟,接下来我们一起尝 ...

  5. springboot 集成shiro

    首先看下shiro configuration 的配置,重要部分用红色标出了 package cn.xiaojf.today.shiro.configuration; import at.pollux ...

  6. 学习笔记TF020:序列标注、手写小写字母OCR数据集、双向RNN

    序列标注(sequence labelling),输入序列每一帧预测一个类别.OCR(Optical Character Recognition 光学字符识别). MIT口语系统研究组Rob Kass ...

  7. 移动端H5通用表单验证插件

    将表单验证的通用部分提炼出来,做成一个简易插件,方便调用. 已将源码放到GitHub上,名字叫zValidate. 手机可扫描下图查看示例,PC端可点击此处查看: 一.原理 1)需要引入zepto.j ...

  8. python中的map、filter、reduce函数

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  9. jquery点击非div区域隐藏div

    点击非div区域隐藏div,如图,点击圆的头像(.person-msg)弹出白色底框(.person-centre).点击圆头像以外的区域隐藏白色底框

  10. Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    Vue的报错:Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>' ...