Python build simple server analysis and implementation _python

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.