"""
Small wsgiref based web server. Takes a path to serve from and an
optional port number (defaults to 8000), then tries to serve files.
MIME types are guessed from the file names, 404 errors are raised
if the file is not found.
"""
import mimetypes
import os
import sys
from wsgiref import simple_server, util
from urllib.parse import parse_qs
def app(environ, respond):
get_query_ =environ['QUERY_STRING']
post_query_=environ["wsgi.input"].read(int(e)).decode("utf-8", 'ignore') if (e:=environ.get("CONTENT_LENGTH")) else None
get_query =parse_qs(get_query_)
post_query=parse_qs(post_query_)
print("REQUEST_METHOD: "+environ['REQUEST_METHOD'])
print("GET : ",(get_query))
print("POST: ",(post_query))
if(environ["PATH_INFO"].startswith(r"/post")):
respond("200 OK", [("Content-Type", "text/html; charset=utf-8")])
message=("Welcome, "+e[0]+" !") if (e:=post_query.get('username')) else "Welcome ! "
html = ''' Post page %s
Post Data:
%s
Path:
%s
''' %( message, str(post_query), environ["PATH_INFO"])
return [html.encode("utf-8")]
else:
# Get the file name and MIME type
fn = os.path.join(path,"res", environ["PATH_INFO"][1:])
if "." not in fn.split(os.path.sep)[-1]:
fn = os.path.join(fn, "index.html")
mime_type = mimetypes.guess_type(fn)[0]
# Return 200 OK if file exists, otherwise 404 Not Found
if os.path.exists(fn):
respond("200 OK", [("Content-Type", mime_type)])
return util.FileWrapper(open(fn, "rb"))
else:
respond("404 Not Found", [("Content-Type", "text/plain")])
return [b"not found"]
if __name__ == "__main__":
# Get the path and port from command-line arguments
path = sys.argv[1] if len(sys.argv) >1 else os.getcwd()
port = int(sys.argv[2]) if len(sys.argv) >2 else 8000
# Make and start the server until control-c
httpd = simple_server.make_server("", port, app)
print(f"Serving {path} on port {port}, control-C to stop")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("Shutting down.")
httpd.server_close()
靜態HTML文件代碼????? download
你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
新聞名稱:【Python】WSGI簡單Web服務器,處理GET和POST請求-創新互聯
瀏覽地址:http://m.kartarina.com/article16/cdcjgg.html
成都網站建設公司_創新互聯,為您提供網站維護、響應式網站、定制開發、微信公眾號、企業網站制作、標簽優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯