????????本周任務:完成http 服務器設計,可以實現(xiàn)api的遠程調用
????????采取這種解決方案的原因:每次調用模型乳附,需要使用重新解釋模型辛蚊,需要耗費較多的時間,因此采用這種方式窟扑,讓模型一直加載在后臺喇颁,仿照網(wǎng)站系統(tǒng),通過get()和post()函數(shù)來傳遞參數(shù)嚎货。
? ? ? ? 選擇python的原因是无牵,我們都是使用python語言來構建模型。
? ? ? ? 首先厂抖,在本機上進行測試茎毁,選用端口號8080
HTTP響應的結構類似于請求:
HTTP method:HTTP請求方法。最常用的就是 GET(抓取數(shù)據(jù))與POST(更新數(shù)據(jù)或者上傳文件)
URL:通常是客戶端請求的文件的路徑忱辅,比如 /research/experiments.html七蜘, 但是是否響應文件都是由服務器決定的。
HTTP version:HTTP版本墙懂。通常是 HTTP/1.0 或 HTTP/1.1
header field:HTTP頭內的鍵值對橡卤,做一些基本設置,就像下面這樣损搬。
客戶端接受的數(shù)據(jù)類型
Accept: text/html
from http.server import HTTPServer, CGIHTTPRequestHandler,BaseHTTPRequestHandler
import urllib
port = 8080
httpd = HTTPServer(('', port), testHTTPServer_RequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()
具體獲取參數(shù)的get()函數(shù)碧库,需要在?testHTTPServer_RequestHandler類中:
class testHTTPServer_RequestHandler(BaseHTTPRequestHandler):
# GET
? ? def do_GET(self):
if '?' in self.path:# 如果帶有參數(shù)
? ? ? ? ? ? self.queryString = urllib.parse.unquote(self.path.split('?',1)[1])
# name=str(bytes(params['name'][0],'GBK'),'utf-8')
? ? ? ? ? ? params = urllib.parse.parse_qs(self.queryString)
print(params)
name = params["name"][0]if "name" in paramselse None
? ? ? ? ? ? # Send response status code
? ? ? ? self.send_response(200)
# Send headers
? ? ? ? self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(bytes("
hi!
","utf8"))# Send message back to client
#message = "Hello world!"
# Write content as utf-8 data
#self.wfile.write(bytes(message, "utf8"))
? ? ? ? return
? ? ? ? 通過柜与,傳遞來的url來獲取需求參數(shù)信息,并且返回輸入結果到客戶端。