Demand Analysis:
Save the number of users have broken 6000, the original static report has become bloated,
Every time you open it, you have to slow it down half even the browser hangs
Using Python to build a most simple Web service request a Nick
Returns the corresponding report data parameter to be transmitted by get way
Research and implementation:
The park did not find a reliable, Google for half a day, eventually succeeded.
The following is the source code, which records some of these problems
Copy Code code as follows:
#! /usr/bin/env python
#-*-Coding:utf-8-*-
"""
@author: Zhoujiebin
@contact: zhoujiebing@maimiaotech.com
@date: 2012-12-14 15:25
@version: 0.0.0
@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 How to get the client's getting parameters
#我找半天没找到, the last __dict__ see paths in the path, can only extract parameters from the path
#从path中提取 Get Parameters
Nick = self.path[1:]
#汉字url转码
Nick = str (Urllib.unquote (Nick))
If Nick!= 1:
report_html = get_html (Nick)
Else
report_html = ' Nick is illegal '
print ' request ' + Nick + ' Save Oil treasure ' plan report
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)
#启动服务器-Side process
Httpd.serve_forever ()
Except Exception,e:
print ' Exception ', E
Execute this program the Web service program is started
You can enter Ip:8080/nick in the browser.