Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
用python搭建web服务器,与ajax交互,接收处理Get和Post请求;简单实用,没有用框架,适用于简单需求,更多功能可进行扩展。
python有自带模块BaseHTTPServer、CGIHTTPServer、SimpleHTTPServer,详细功能可参考API
前台html:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>test</h1>
<p>this is a test page!</p>
<button onclick="get()">click</button>
<div id="result"></div>
<script src="libs\jquery-3.2.1.min.js"></script>
<script>
function get(){
//alert("test");
$.ajax({
url:"BaseInfo",
data:{id:123,name:"xiaoming"},
success:function(e){
$("#result").html(e);
}})
}
</script>
</body>
</html>
python代码:
#!coding:utf8
import BaseHTTPServer
import CGIHTTPServer
import SimpleHTTPServer
import SocketServer
import urllib
import io
import shutil PORT=8000 #定义数据处理模块--此部分可放于外部引用文件
class dataHandler():
#接口分发
def run(self,path,args):
index = path.replace("/","")
switch={
"BaseInfo": self.getBaseInfo,
"Monitor": self.getMonitor
}
return switch[index](args)
#接口具体实现
def getBaseInfo(self,args):
return "BaseInfo:"+args
def getMonitor(self,args):
return "Monitor"+args #服务环境搭建
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
mpath,margs=urllib.splitquery(self.path) # ?分割
if margs==None:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
else:
self.do_action(mpath, margs)
def do_POST(self):
mpath,margs=urllib.splitquery(self.path)
datas = self.rfile.read(int(self.headers['content-length']))
self.do_action(mpath, datas)
#请求处理方法
def do_action(self, path, args):
dh = dataHandler()
result = dh.run(path, args)
self.outputtxt(result)
#数据返回到前台
def outputtxt(self, content):
#指定返回编码
enc = "UTF-8"
content = content.encode(enc)
f = io.BytesIO()
f.write(content)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
shutil.copyfileobj(f,self.wfile)
#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) #web服务主程序
httpd = SocketServer.TCPServer(("", PORT), ServerHandler)
print "serving at port", PORT
httpd.serve_forever()
部分代码内容参考网友,整理仅供学习交流,欢迎留言交流。
Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构的更多相关文章
- Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程
Ubuntu 搭建Web服务器(MySQL+PHP+Apache)详细教程 看了好多人的博客,有的不全 or 有问题,整理了一下,适合小白 新手先整理几个小问题 1.为啥使用 Linux 搭建服务器? ...
- 记录一些服务端术语和搭建web服务器
菜单快捷导航 服务端常用术语 搭建web服务器和配置虚拟主机 记录一些服务端方面的常用术语 1.CS架构和BS架构 1.1 CS架构 CS(Client/Server),基于安装包类型的桌面或手机软件 ...
- 使用 Node.js 搭建 Web 服务器
使用Node.js搭建Web服务器是学习Node.js比较全面的入门教程,因为实现Web服务器需要用到几个比较重要的模块:http模块.文件系统.url解析模块.路径解析模块.以及301重定向技术等, ...
- 在国外搭建 Web 服务器 - Linode VPS
在国外搭建 Web 服务器 - Linode VPS 买一台虚拟服务器(VPS),把你网站放在上面跑跑,找找感觉,平时也可以用它来练习.前几天,搜索到了有人推荐 Linode 的 VPS,昨天又有朋友 ...
- 在Win7系统中搭建Web服务器
局 域网Web服务器的主要功能是实现资源共享,同时借助于局域网服务器访问页面可有效的实现信息的同步.利用Web服务器,我们随时随地都可以将自己的信息 上传到服务器端,让其它关注你的用户能在第一时间内了 ...
- 轻松使用Nginx搭建web服务器
如果读者以前做过web开发的话,就应该知道如何去搭建一个web服务器来跑你的web站点,在windows下你可能会选择去用IIS,十分的快捷,在linux下,你可能首先会想到apache,“一哥”( ...
- 用tomcat搭建web服务器
链接地址:http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html qingshow “不积跬步无以至千里,不积小流无以成江海”. ...
- NodeMCU入门(4):搭建Web服务器,配置网络连接
准备工作 1.NodeMCU模块 2.ESPlorer v0.2.0-rc6 3.NodeMCU-HTTP-Server 搭建web服务器 下载https://github.com/wangzexi/ ...
- CentOS 6.2下搭建Web服务器
1Centos 6.2下搭建web服务器 如今,Linux在Web应用越来越广,许多企业都采用Linux来搭建Web服务器,这样即节省了购买正版软件的费用,而且还能够提高服务器的安全性. 之前我们介绍 ...
随机推荐
- Java语言写出水仙花数,
package com.llh.demo;/** * 水仙花数 * @author llh * */public class Demo14 { public static void main(S ...
- css超过一定长度显示省略号
overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
- JavaScript 和 TypeScript 交叉口 —— 类型定义文件(*.d.ts)
在 <从 JavaScript 到 TypeScript 系列> 文章我们已经学习了 TypeScript 相关的知识. TypeScript 的核心在于静态类型,我们在编写 TS 的时候 ...
- Mybatis动态查询语句
MyBatis中动态SQL语句完成多条件查询 标签: mybatis动态SQL多条件查询java.sql.SQLSyntaxEr 2015-06-29 19:00 22380人阅读 评论(0) 收藏 ...
- 原生 drag drop HTML5
drag事件( dragstart -- drag -- dragend ) 当按下鼠标开始drag一个可以拖动的对象时,触发dragstart事件,如果元素是不可拖动的话,会出现一个不可拖动的图 ...
- mybatis映射异常
今天写项目突然遇到了这么个问题: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no ...
- jquery IE6 下animate 动画的opacity无效
jquery IE6 下animate 动画的opacity无效,其实是有效的,因为IETester的一个小BUG 原生IE6 没问题...呵呵~~
- 一起写框架-Ioc内核容器的实现-基础API的定义(三)
Ioc内核要解决的问题 1.被调用方,在程序启动时就要创建好对象,放在一个容器里面. 2.调用方使用一个接口或类的引用(不用使用new),就可以创建获得对象. 解决这个两个问题的思路 1.定义一个对象 ...
- Hibernate开发文档
hibernate配置 映射约束文件 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3. ...
- 《Python数据分析常用手册》一、NumPy和Pandas篇
一.常用链接: 1.Python官网:https://www.python.org/ 2.各种库的whl离线安装包:http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...