#-*- 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. ftp服务器可以连接但不能传输数据(proftpd)

    问题:在客户端连接FTP服务器(proftpd)时可以正常连接,但是无法正常传输数据 ftp> ls530 Please login with USER and PASSPassive mode ...

  2. [原创]KVM虚拟化实践记录

    一.KVM简介KVM是开源软件,全称是kernel-based virtual machine(基于内核的虚拟机),是一个开源的系统虚拟化模块,基于硬件的完全虚拟化,不过需要硬件支持(如Intel V ...

  3. (数字IC)低功耗设计入门(五)——RTL级低功耗设计(续)

    二.RTL级低功耗设计(续) 前面一篇博文我记录了操作数隔离等低功耗设计,这里就主要介绍一下使用门控时钟进行低功耗设计. (4)门控时钟 门控时钟在我的第一篇博客中有简单的描述,这里就进行比较详细的描 ...

  4. R语言进行文件夹操作示例(转)

    rm(list=ls())path = 'J:/lab/EX29 --在R语言中进行文件(夹)操作'setwd(path)cat("file A\n", file="A& ...

  5. Lock(二)解决Lock问题

    本文介绍通过Toad.EM及SQL语句来处理数据库产生的锁.在这之前需要对v$lock和v$session这两个数据字典有一定的了解. (一)使用Toad处理锁 (1)使用Toad的session b ...

  6. ThinkPHP5.0更改框架的验证方法对象->validate(true)->save();

    我们更希望看到: // 新增对象至数据表 $result = $Teacher->validate(true)->save(); 而不是: // 新增对象至数据表 $result = $T ...

  7. windows 配置 Scheme + Emacs 编程环境

    软件下载列表: Emacs Racket (这里使用 Racket ,更加方便,便于后面配置 Emacs) 配置 安装好 Emacs 后,在 C:\Users\用户名\AppData\Roaming\ ...

  8. SpringMVC 整合Jackson报错

    最近用spring4.x整合Jackson,结果莫名其妙的一直报错,网上收索的结果都是在maven或者gradle的环境下配置依赖条件解决的:但是eclipseIDE环境下的jar包应该是会自动依赖影 ...

  9. FastReport.NET 中使用二维码

    FastReport.net 是一个比较好用的报表控件,在编辑器中编辑以后 可以直接在vs 中引用. 最近在研究fastreport 现在讲解一下 如何使用它的二维码. fastreport 没有单独 ...

  10. Javascript数组操作详细解答

    数组push()方法向数组尾部追加新元素,返回值为新数组的长度;括号里面带新追加的元素pop()方法从数组尾部移除一个元素,返回值为移除的元素括号里面不能带参数 shift()方法从数组头部移除一个元 ...