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 ...
随机推荐
- Automated Front End Test - Xvfb, Chromedriver, Selenium, Jenkins
1. Install Xvfbm, google-chrome-stable and chromedriver in Jenkins sudo apt-get install -y xvfb goog ...
- TWaver 2D+GIS+3D的试用和在线Demo
TWaver 2D for HTML5试用下载: http://download.servasoft.com/dl/twaver/sssyuwyeriUR/k/twaver-html5-5.4.7.z ...
- RedHat7上安装MySQL5.7.16
1.查看系统中是否已将安装MySQL,如果安装了,需要卸载. [root@chenguo etc]# rpm -qa|grep -i mysql 2.创建用户和组 [root@chenguo ~]# ...
- js实现分页
<html> <head> <meta charset='utf-8'> <script type="text/javascript" s ...
- Node.js 8有哪些重要功能和修复?
欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~ 5月30日12点,Node.js 8正式发布了,这个版本具有一系列新功能和性能改进,并且这些功能和改进将获得长期支持(LTS). ...
- 使用VideoView开发视频总结
一.VideoView及其相关组件总结 在Android中,播放视频有2种方式,第一种方式是使用MediaPlayer结合SurfaceView来播放,通过MediaPlayer来控制视频的播放.暂停 ...
- js写插件教程
<!doctype html><html lang="en"><head> <meta charset="UTF-8&qu ...
- poj1379
poj1379 题意 给出 n 个洞的坐标,要求找到一点使得这一点距离最近洞的距离最远. 分析 通过这道题学习一下模拟退火算法, 这种随机化的算法,在求解距离且精度要求较小时很有用. 简而言之,由随机 ...
- Vue单页式应用(Hash模式下)实现微信分享
前端微信分享的基本步骤: 一.绑定域名: 先登录微信公众平台进入"公众号设置"的"功能设置"里填写"JS接口安全域名".这个不多说,微信开发 ...
- 【Python3之常用模块】
一.time 1.三种表达方式 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.命令如下 ...