快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

【Python】WSGI简单Web服务器,处理GET和POST请求-创新互联

【Python】WSGI简单Web服务器,处理GET和POST请求
    • Python服务器代码
    • 静态HTML文件代码

创新互联建站成立与2013年,是专业互联网技术服务公司,拥有项目网站设计制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元珠晖做网站,已为上家服务,为珠晖各地企业和个人服务,联系电话:13518219792Python服务器代码
"""
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文件代码
⭐⭐⭐⭐⭐
username:
password:
文本框:
download

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页标题:【Python】WSGI简单Web服务器,处理GET和POST请求-创新互联
本文URL:http://6mz.cn/article/cdcjgg.html

其他资讯