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服务器,这样即节省了购买正版软件的费用,而且还能够提高服务器的安全性. 之前我们介绍 ...
随机推荐
- SpringMVC自定义配置消息转换器踩坑总结
问题描述 最近在开发时候碰到一个问题,springmvc页面向后台传数据的时候,通常我是这样处理的,在前台把数据打成一个json,在后台接口中使用@requestbody定义一个对象来接收,但是这次数 ...
- HDU 5182
#include <iostream> #include <algorithm> #include <cstring> using namespace std; / ...
- Chinese Rings
Chinese Rings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [DeeplearningAI笔记]ML strategy_2_3迁移学习/多任务学习
机器学习策略-多任务学习 Learninig from multiple tasks 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.7 迁移学习 Transfer Learninig 神 ...
- Matrix 矩阵
CSS3中的矩阵指的是一个方法,书写为matrix()和matrix3d(),前者是元素2D平面的移动变换(transform),后者则是3D变换.2D变换矩阵为3*3, 如上面矩阵示意图:3D变换则 ...
- jQuery选择器(ID选择器)第一节
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 万能日志数据收集器 Fluentd - 每天5分钟玩转 Docker 容器技术(91)
前面的 ELK 中我们是用 Filebeat 收集 Docker 容器的日志,利用的是 Docker 默认的 logging driver json-file,本节我们将使用 fluentd 来收集容 ...
- spring boot hello and docker
主要是想试下spring boot运行在docker里的感觉, 小试牛刀 :) 这是原文,参考一下: https://spring.io/guides/gs/spring-boot-docker ...
- 【机器学习实战】第14章 利用SVD简化数据
第14章 利用SVD简化数据 SVD 概述 奇异值分解(SVD, Singular Value Decomposition): 提取信息的一种方法,可以把 SVD 看成是从噪声数据中抽取相关特征.从生 ...
- 基于python3.x,使用Tornado中的torndb模块操作数据库
目前Tornado中的torndb模块是不支持python3.x,所以需要修改部分torndb源码即可正常使用 1.开发环境介绍 操作系统:win8(64位),python版本:python3.6(3 ...