日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python BaseHTTPServer 模块解析

發布時間:2023/12/20 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python BaseHTTPServer 模块解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#-*- encoding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8')# @explain: 實現GET方法和POST方法請求from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler import urllibclass ServerHTTP(BaseHTTPRequestHandler):def do_GET(self):#針對GET請求方式的應答函數print"----------------------------------GET----------------------------------"path = self.path#127.0.0.1:8000/wahaha提取出路徑的后面部分print path#拆分url(也可根據拆分的url獲取Get提交才數據),可以將不同的path和參數加載不同的html頁面,或調用不同的方法返回不同的數據,來實現簡單的網站或接口query = urllib.splitquery(path)print "query=",queryself.send_response(200)self.send_header("Content-type","text/html")self.send_header("test","This is test!")self.end_headers()buf = '''<!DOCTYPE HTML><html><head><title>Get page</title></head><body><form action="post_page" method="post">username: <input type="text" name="username" /><br />password: <input type="text" name="password" /><br /><input type="submit" value="POST" /></form></body></html>'''self.wfile.write(buf)#GET方法對應的請求方式 curl -i 127.0.0.1:8000/wahahadef do_POST(self):#針對post請求方式的應答函數print"---------------------------------POST---------------------------------"path = self.pathprint path#獲取post提交的數據datas = self.rfile.read(int(self.headers['content-length']))datas = urllib.unquote(datas).decode("utf-8", 'ignore')self.send_response(200)self.send_header("Content-type","text/html")self.send_header("test","This is test!")self.end_headers()buf = '''<!DOCTYPE HTML><html><head><title>Post page</title></head><body>Post Data:%s <br />Path:%s</body></html>'''%(datas,self.path)self.wfile.write(buf)#curl -l -H "Content-type: application/json" -X POST -d '{"phone":"13521389587","password":"test"}' 127.0.0.1:8000/wahahadef start_server(port):http_server = HTTPServer(('', int(port)), ServerHTTP)#HTTPServer綁定對應的應答類ServerHTTPhttp_server.serve_forever()if __name__ == "__main__":start_server(8000)#監聽端口


瀏覽器窗口輸入:127.0.0.1:8000 + /anyurl 也可不加,加了后會打印對應的url

BaseHTTPServer:

主要包含兩個類HTTPServerBaseHTTPRequestHandler

HTTPServer:
??? 繼承SocketServer.TCPServer,用于獲取請求,并將請求分配給應答程序處理

BaseHTTPRequestHandler:
??? 繼承SocketServer.StreamRequestHandler,對http連接的請求作出應答(response)
?? ?
基于BaseHTTPServer 的Http Server的處理流程:
1.HTTPServer綁定對應的應答類(BaseHTTPRequestHandler )
??? http_server = HTTPServer(('', int(port)), ServerHTTP)
2.監聽端口:
??? http_server.serve_forever()?
??? serve_forever()方法使用select.select()循環監聽請求,當接收到請求后調用
??? 當監聽到請求時,取出請求對象
3.應答:
??? 創建新線程以連接對象(開始理解成請求對象)為參數實例化應答類:ServerHTTP()
??? 應答類根據請求方式調用ServerHTTP.do_XXX處理方法

常用方法/屬性:

BaseHTTPRequestHandler.path??????????????????? #包含的請求路徑和GET請求的數據
BaseHTTPRequestHandler.command???????????????? #請求類型GET、POST...
BaseHTTPRequestHandler.request_version???????? #請求的協議類型HTTP/1.0、HTTP/1.1
BaseHTTPRequestHandler.headers???????????????? #請求的頭
BaseHTTPRequestHandler.responses?????????????? #HTTP錯誤代碼及對應錯誤信息的字典
BaseHTTPRequestHandler.handle()??????????????? #用于處理某一連接對象的請求,調用handle_one_request方法處理
BaseHTTPRequestHandler.handle_one_request()??? #根據請求類型調用do_XXX()方法,XXX為請求類型
BaseHTTPRequestHandler.do_XXX()??????????????? #處理請求
BaseHTTPRequestHandler.send_error()??????????? #發送并記錄一個完整的錯誤回復到客戶端,內部調用send_response()方法實現
BaseHTTPRequestHandler.send_response()???????? #發送一個響應頭并記錄已接收的請求
BaseHTTPRequestHandler.send_header()?????????? #發送一個指定的HTTP頭到輸出流。 keyword 應該指定頭關鍵字,value 指定它的值
BaseHTTPRequestHandler.end_headers()?????????? #發送一個空白行,標識發送HTTP頭部結束
BaseHTTPRequestHandler.wfile??? #self.connection.makefile('rb', self.wbufsize) self.wbufsize = -1 應答的HTTP文本流對象,可寫入應答信息
BaseHTTPRequestHandler.rfile??? #self.connection.makefile('wb', self.rbufsize) self.rbufsize = 0? 請求的HTTP文本流對象,可讀取請求信息

更多屬性和方法可查看模塊源碼

總結

以上是生活随笔為你收集整理的Python BaseHTTPServer 模块解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。