python webserver, based on SimpleHTTPServer
#-*- 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的更多相关文章
- python标准库学习-SimpleHTTPServer
这是一个专题 记录学习python标准库的笔记及心得 简单http服务 SimpleHTTPServer 使用 python -m SimpleHTTPServer 默认启动8000端口 源码: &q ...
- 通过Python自带模块SimpleHTTPServer快速共享服务的配置文件
简介 SimpleHTTPServer是Python 2自带的一个模块,是Python的Web服务器,简单小巧,快速启动. 它在Python 3已经合并到http.server模块中. SimpleH ...
- Python WebServer with MSSql
今天尝试了一下在windows上用python来写web服务 我的环境是 win7(64位)+ python(2.7.11) 第一步需要安装pymssql 第二步需要安装tornado(web服务靠他 ...
- python webserver客户端
1.库 suds库,只能做webserver客户端,轻量化,使用方便.安装使用pip. 2.使用 如有webserver情况如下: url:http://10.110.35.41:8980/wsser ...
- 【Python 开发】第三篇:python 实用小工具
一.快速启动一个web下载服务器 官方文档:https://docs.python.org/2/library/simplehttpserver.html 1)web服务器:使用SimpleHTTPS ...
- 【转】Python资源 转自 dylanninin.com
http://dylanninin.com/blog/2013/11/23/python_resource.html Python是在工作期间零零碎碎学习起来的.当时正值部门申购图书,鉴于Python ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
- Machine and Deep Learning with Python
Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...
随机推荐
- elasticsearch系列(四)部署
本文采用tar包的方式部署es 准备jdk8的环境 5.4.0的es依赖jdk8及以上版本 下载linux版的jdk jdk-8u121-linux-x64.tar.gz tar -zvxf jdk- ...
- eclipse内存溢出设置
1. arguments中的内容添加红色部分: -Dcatalina.base="E:\workspace\.metadata\.plugins \org.eclipse.wst.s ...
- javaWeb学习总结(4)- HttpServletResponse
一.简介: Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和 ...
- 003---hibernate主要接口介绍
Hibernate可以访问JNDI.JDBC.JTA JNDI(Java名称和目录接口):主要管理我们对象,特别是EJB应用,它会把所有EJB应用加入到JNDI这棵树上,Tomcat连接池也是把对象注 ...
- 使用Charles Proxy提升iOS开发效率
以前做前端开发的时候,使用最多的工具就是 Fiddler ,用来定位问题.模拟特定场景非常方便,极大提升了开发效率.而转做 iOS 开发以后,一大头疼的问题是 Fiddler 没有 Mac 版,幸亏找 ...
- 编写原生的Node.js模块
导语:当Javascript的性能遭遇瓶颈,或者需要增强Javascript能力的时候,就需要依赖native模块来实现了. 应用场景 日常工作中,我们经常需要将原生的Node.js模块做为依赖并在项 ...
- css3学习系列之选择器(一)
CSS3中的属性选择器 [att*=val]属性选择器:[att*=val]属性选择器的含义是:如果元素att表示的属性之属性值中包含用val指定的字符的话,则该元素使用这个样式. [att^=val ...
- window maven批量删除.lastUpdated文件
当下载网络上的jar包 网络不通 中途中断 会产生.lastUpdated,maven就不在从网上下载jar包了 很烦 ~~~ 执行下面的批处理程序即可 @echo off set REPOSITOR ...
- 脑洞大开--一条项目中常用的linux命令引发的经典算法题
小时候家里定了<读者>的月刊,里面记录一个故事:说有有个偏僻的乡村一日突然来了一个美女,她携着万贯家财子女在当地安家落户,成了当地的乡绅.她让她的子女世世代代的保守这个秘密,直到这个秘密不 ...
- 执行ANT JAVA三种方式
1. 命令行 <target name="reporttoexcel" depends="report"> <exec executable= ...