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 ...
随机推荐
- 使用Linux的alternatives命令替换选择软件的版本
上周在安装搜索引擎Elasticsearch时,要求安装比较新的java 版本,我选择了java 1.8.0,安装java 成功后使用java -version 发现使用的版本仍旧是1.6.0, 查询 ...
- wiringPi安装
wiringPi安装 更新软件,输入以下指令: sudo apt-get update sudo apt-get upgrade 通过GIT获得wiringPi的源代码 git c ...
- 多媒体开发库 之 SDL 详解
SDL 简介 SDL(Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的 ...
- Android学习笔记View的工作原理
自定义View,也可以称为自定义控件,通过自定义View可以使得控件实现各种定制的效果. 实现自定义View,需要掌握View的底层工作原理,比如View的测量过程.布局流程以及绘制流程,除此之外,还 ...
- [Leetcode] Binary search--436. Find Right Interval
Given a set of intervals, for each of the interval i, check if there exists an interval j whose st ...
- PHP漏洞之session会话劫持
本文主要介绍针对PHP网站Session劫持.session劫持是一种比较复杂的攻击方法.大部分互联网上的电脑多存在被攻击的危险.这是一种劫持tcp协议的方法,所以几乎所有的局域网,都存在被劫持可能. ...
- Spring学习(6)---Bean定义及作用域的例子
(一)Bean的定义 先定义一个BeanAnnotation package com.mypackage; import org.springframework.stereotype.Componen ...
- jQuery 插件 的this 指向问题(实战)
daterangepicker 是一个JavaScript组件,用来选择日期. 资源直接搜索 daterangepicker 即可,当然好看的样式可以基于Bootstrap. 官网:http://ww ...
- 【原】vue单文件组件互相通讯
在vue中,我们可以把一个页面各个部分单独封装起来,做成各种小组件,然后加载,这样的好处是代码维护起来比较容易,重复的代码多处调用! 在一个vue项目中,单文件组件以.vue形式文件命名 每个组件之间 ...
- 友盟分享到微信 监听不执行 监听只执行onStart,(onResult,onError,onCancel 不执行)
最近在做一个项目 有一个需求是要分享项目中的一个商品 这对于我来说简直是 so easy (项目是三个人一起写的) 正好看到之前有同事写完了 我就拿过来用吧 一顿复制粘贴 大功告成 这个是监 ...