python搭建簡易服務器分析與實現(xiàn)
更新時間:2012年12月15日 15:51:21 作者:
本文將介紹python搭建簡易服務器實現(xiàn)步驟,需要了解的朋友可以參考下
需求分析:
省油寶用戶數(shù) 已經破了6000,原有的靜態(tài)報表 已經變得臃腫不堪,
每次打開都要緩上半天,甚至瀏覽器直接掛掉
采用python搭建一個最最簡易的 web 服務 請求一個nick
就返回 對應的 報表數(shù)據 參數(shù)用GET方式傳送
調研與實現(xiàn):
園里沒找到靠譜的,google了半天,最終還是成功了。
以下是源碼,里面記錄了 其中的 一些問題
#! /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 如何拿到客戶端的GET參數(shù)
#我找半天沒找到,最后__dict__看到path里有路徑,只能從路徑里 提取參數(shù)了
#從path中提取 GET參數(shù)
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
執(zhí)行這個程序 web服務程序 就啟動了
在瀏覽器中 輸入 ip:8080/nick 就可以了
省油寶用戶數(shù) 已經破了6000,原有的靜態(tài)報表 已經變得臃腫不堪,
每次打開都要緩上半天,甚至瀏覽器直接掛掉
采用python搭建一個最最簡易的 web 服務 請求一個nick
就返回 對應的 報表數(shù)據 參數(shù)用GET方式傳送
調研與實現(xiàn):
園里沒找到靠譜的,google了半天,最終還是成功了。
以下是源碼,里面記錄了 其中的 一些問題
復制代碼 代碼如下:
#! /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 如何拿到客戶端的GET參數(shù)
#我找半天沒找到,最后__dict__看到path里有路徑,只能從路徑里 提取參數(shù)了
#從path中提取 GET參數(shù)
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
執(zhí)行這個程序 web服務程序 就啟動了
在瀏覽器中 輸入 ip:8080/nick 就可以了
您可能感興趣的文章:
相關文章
Python中urllib與urllib2模塊的變化與使用詳解
urllib是python提供的一個用于操作URL的模塊,在python2.x中有URllib庫,也有Urllib2庫,在python3.x中Urllib2合并到了Urllib中,我們爬取網頁的時候需要經常使用到這個庫,需要的朋友可以參考下2023-05-05Python pandas.DataFrame調整列順序及修改index名的方法
這篇文章主要介紹了Python pandas.DataFrame調整列順序及修改index名的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06Python?numpy中np.random.seed()的詳細用法實例
在學習人工智能時,大量的使用了np.random.seed(),利用隨機數(shù)種子,使得每次生成的隨機數(shù)相同,下面這篇文章主要給大家介紹了關于Python?numpy中np.random.seed()的詳細用法,需要的朋友可以參考下2022-08-08Flask框架實現(xiàn)給視圖函數(shù)增加裝飾器操作示例
這篇文章主要介紹了Flask框架實現(xiàn)給視圖函數(shù)增加裝飾器操作,結合實例形式分析了flask框架視圖添加裝飾器的具體操作方法及相關注意事項,需要的朋友可以參考下2018-07-07詳解利用python-highcharts庫繪制交互式可視化圖表
本文主要和大家分享一個超強交互式可視化繪制工具-python-highcharts。python-highcharts就是使用Python進行Highcharts項目繪制,簡單的說就是實現(xiàn)Python和Javascript之間的簡單轉換層,感興趣的可以了解一下2022-03-03