
WechatIMG62.jpeg
環(huán)境
macOS 10.14.6
Python 3.7.7
httpie
httpie 是一個命令行 HTTP 客戶端。
安裝httpie
- 安裝
$ brew install httpie
這里使用 homebrew 安裝 httpie。
- 查看版本
$ http --version
2.2.0
代碼
#-*- coding:utf-8 -*-
import sys, os, urllib, subprocess
import urllib.parse
import urllib.request
from http.server import BaseHTTPRequestHandler,HTTPServer
#-------------------------------------------------------------------------------
class ServerException(Exception):
'''服務(wù)器內(nèi)部錯誤'''
pass
#-------------------------------------------------------------------------------
class base_case(object):
'''條件處理基類'''
def handle_file(self, handler, full_path):
try:
with open(full_path, 'rb') as reader:
content = reader.read()
handler.send_content(content)
except IOError as msg:
msg = "'{0}' cannot be read: {1}".format(full_path, msg)
handler.handle_error(msg)
def index_path(self, handler):
return os.path.join(handler.full_path, 'index.html')
def test(self, handler):
assert False, 'Not implemented.'
def act(self, handler):
assert False, 'Not implemented.'
#-------------------------------------------------------------------------------
class case_no_file(base_case):
'''文件或目錄不存在'''
def test(self, handler):
return not os.path.exists(handler.full_path)
def act(self, handler):
raise ServerException("'{0}' not found".format(handler.path))
#-------------------------------------------------------------------------------
class case_cgi_file(base_case):
'''可執(zhí)行腳本'''
def run_cgi(self, handler):
data = subprocess.check_output(["python3", handler.full_path],shell=False)
handler.send_content(data)
def test(self, handler):
return os.path.isfile(handler.full_path) and \
handler.full_path.endswith('.py')
def act(self, handler):
self.run_cgi(handler)
#-------------------------------------------------------------------------------
class case_existing_file(base_case):
'''文件存在的情況'''
def test(self, handler):
return os.path.isfile(handler.full_path)
def act(self, handler):
self.handle_file(handler, handler.full_path)
#-------------------------------------------------------------------------------
class case_directory_index_file(base_case):
'''在根路徑下返回主頁文件'''
def test(self, handler):
return os.path.isdir(handler.full_path) and \
os.path.isfile(self.index_path(handler))
def act(self, handler):
self.handle_file(handler, self.index_path(handler))
#-------------------------------------------------------------------------------
class case_always_fail(base_case):
'''默認處理'''
def test(self, handler):
return True
def act(self, handler):
raise ServerException("Unknown object '{0}'".format(handler.path))
#-------------------------------------------------------------------------------
class RequestHandler(BaseHTTPRequestHandler):
'''
請求路徑合法則返回相應(yīng)處理
否則返回錯誤頁面
'''
Cases = [case_no_file(),
case_cgi_file(),
case_existing_file(),
case_directory_index_file(),
case_always_fail()]
# 錯誤頁面模板
Error_Page = """\
<html>
<body>
<h1>Error accessing {path}</h1>
<p>{msg}</p>
</body>
</html>
"""
# 處理get請求
def do_GET(self):
try:
# 得到完整的請求路徑
self.full_path = os.getcwd() + self.path
# 遍歷所有的情況并處理
for case in self.Cases:
if case.test(self):
case.act(self)
break
# 處理異常
except Exception as msg:
self.handle_error(msg)
# 處理post請求
def do_POST(self):
datas = self.rfile.read(int(self.headers['content-length']))
self.send_content(datas)
# 處理異常
def handle_error(self, msg):
content = self.Error_Page.format(path=self.path, msg=msg)
self.send_content(content.encode("utf-8"), 404)
# 發(fā)送數(shù)據(jù)到客戶端
def send_content(self, content, status=200):
self.send_response(status)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(len(content)))
self.end_headers()
self.wfile.write(content)
#-------------------------------------------------------------------------------
if __name__ == '__main__':
serverAddress = ('', 8080)
server = HTTPServer(serverAddress, RequestHandler)
server.serve_forever()
創(chuàng)建 server.py,內(nèi)容如上。
運行
- 啟動服務(wù)器
$ python server.py &
&表示將這個任務(wù)放到后臺去執(zhí)行。
- get 請求
$ http 0.0.0.0:8080/home.html
HTTP/1.0 404 Not Found
Content-Length: 152
Content-type: text/html
Date: Thu, 13 Aug 2020 06:22:48 GMT
Server: BaseHTTP/0.6 Python/3.7.7
<html>
<body>
<h1>Error accessing /home.html</h1>
<p>'/home.html' not found</p>
</body>
</html>
- post 請求
$ http 0.0.0.0:8080 hello=world
HTTP/1.0 200 OK
Content-Length: 18
Content-type: text/html
Date: Thu, 13 Aug 2020 06:07:27 GMT
Server: BaseHTTP/0.6 Python/3.7.7
{
"hello": "world"
}