python搭建简易服务器实例参考
有关python搭建简易服务器的方法。
需求分析:
省油宝用户数 已经破了6000,原有的静态报表 已经变得臃肿不堪,
每次打开都要缓上半天,甚至浏览器直接挂掉
采用python搭建一个最最简易的 web 服务 请求一个nick
就返回 对应的 报表数据 参数用GET方式传送
调研与实现:
园里没找到靠谱的,google了半天,最终还是成功了。
以下是源码,里面记录了 其中的 一些问题
复制代码 代码如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: zhoujiebin
@contact: zhoujiebing@maimiaotech.com
@date: 2012-12-14 15:25
@version: 0.0.0 www.jbxue.com
@license: Copyright maimiaotech.com
@copyright: Copyright maimiaotech.com
"""
import os
import sys
import urllib
import SimpleHTTPServer
import SocketServer
PORT = 8080
WEBDIR = "/home/zhoujiebing/report_web_service"
from syb_report_html import get_html
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def translate_path(self, path):
#用于设定根目录
os.chdir(WEBDIR)
SimpleHTTPServer.SimpleHTTPRequestHandler.translate_path(self,path)
def do_GET(self):
#服务器端响应GET请求的方法
#问题1 如何拿到客户端的GET参数
#我找半天没找到,最后__dict__看到path里有路径,只能从路径里 提取参数了
#从path中提取 GET参数
nick = self.path[1:]
#汉字url转码
nick = str(urllib.unquote(nick))
if nick != 1:
report_html = get_html(nick)
else:
report_html = 'nick非法'
print '请求 ' + nick + ' 省油宝计划报表'
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(report_html))
self.end_headers()
self.wfile.write(report_html)
if __name__ == '__main__':
try:
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "dir %s serving at port %s"%(repr(WEBDIR), PORT)
#启动服务器 端进程
httpd.serve_forever()
except Exception,e:
print '异常',e
执行这个程序 web服务程序 就启动了 。
在浏览器中 输入 ip:8080/nick 即可。
python搭建简易服务器实例参考的更多相关文章
- [容器]python搭建简易服务器+docker导入多个镜像shell脚本
从其他机器导出来的docker镜像,集中地放在某台上,其他的机器执行 curl xxx:8000/load_images.sh 来导入镜像,简单方便 使用python简易web服务器. (在镜像目录下 ...
- Python搭建Web服务器,与Ajax交互,接收处理Get和Post请求的简易结构
用python搭建web服务器,与ajax交互,接收处理Get和Post请求:简单实用,没有用框架,适用于简单需求,更多功能可进行扩展. python有自带模块BaseHTTPServer.CGIHT ...
- python搭建本地服务器
python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...
- Python使用socketServer包搭建简易服务器过程详解
官方提供了socketserver包去方便我们快速的搭建一个服务器框架. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的 ...
- python3+socket搭建简易服务器
踩了一上午的坑之后,终于对网络编程有了一点大致的.基本的了解.真的是0基础,之前对socket网络编程一点都不知道.(感觉自己与时代脱轨....) 首先我想对这些美妙的专业术语进行一番搜索: 服务器: ...
- 使用Python搭建http服务器
David Wheeler有一句名言:“计算机科学中的任何问题,都可以通过加上另一层间接的中间层解决.”为了提高Python网络服务的可移植性,Python社区在PEP 333中提出了Web服务器网关 ...
- python 搭建ftp服务器
代码示例: # coding: utf-8 import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.han ...
- python搭建ftp服务器
1 # coding: utf-8 import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handler ...
- Python搭建简易HTTP服务(3.x版本和2.x版本的)
废话不多说,我们工作时经常会用到需要HTTP服务,如果不想搞那些复杂的Apache.IIS服务器等,这时我们就可以用python帮我们搭建一个简单的服务器.操作如下: 1.下载并安装一个python: ...
随机推荐
- Java 程序内存分析
转自:http://www.iteye.com/topic/528230 java程序内存主要分为了2个部分,包括stack segment(栈内存区).heap segment(堆内存区). 在分析 ...
- Swift 2 语言精要 - Initialization and Deinitialization
init相当于构造函数 deinit相当于析构函数 class InitAndDeinitExample { // Designated (i.e., main) initializer init ( ...
- Python编程-Office操作-操作Excel(上)
首先,需要安装openpyxl库 http://openpyxl.readthedocs.org/en/default/ pyton 2.xpip install openpyxl python 3. ...
- Java的类名与文件名必须一致(转)
原文:http://blog.csdn.net/shaoxiaoning/article/details/40424087 1.Java保存的文件名必须与类名一致: 2.如果文件中只有一个类,文件名必 ...
- tomcat启用压缩的方式
<Connector port="7070" protocol="HTTP/1.1"connectionTimeout="20000" ...
- @Value 和 @ConfigurationProperties 获取值的比较
1.不同点 (1)@ConfigurationProperties(prefix = "person") 功能:批量注入配置文件中的属性 SpEL:不支持表达式 JSR303数据校 ...
- 解决 jersey 单jar包 IME media type text/plain was not found.
1.maven-assembly-plugin 换成 --> maven-shade-plugin <plugins> <!-- shade插件打包成jar包 --> ...
- eclipse Failed to load the JNIshared library
eclipse Failed to load the JNIshared library CreateTime--2018年4月22日22:08:35 Author:Marydon 1.情景再现 ...
- 〖Android〗/system/etc/audio_policy.conf
原文件注释说明: # audio hardware module section: contains descriptors for all audio hw modules present on t ...
- 新浪微博API使用初步介绍——解决回调地址的问题
# -*- coding: utf-8 -*- #python 27 #xiaodeng #新浪微博API使用初步介绍——解决回调地址的问题 #http://blog.csdn.net/monsion ...