o [d3@s4dZddlmZmZddlZddlZddlmZddl m Z dZ gdZ de Z e d ejdZe d eZGd d d eZGd ddeZGdddeZddZeefddZedkredde'ZejZededdeddddlZedeWddS1swYdSdS)a!BaseHTTPServer that implements the Python WSGI protocol (PEP 3333) This is both an example of how WSGI can be implemented, and a basis for running simple web applications on a local machine, such as might be done when testing or debugging an application. It has not been reviewed for security issues, however, and we strongly recommend that you use a "real" web server for production use. For example usage, see the 'if __name__=="__main__"' block at the end of the module. See also the BaseHTTPServer module docs for other API information. )BaseHTTPRequestHandler HTTPServerN) SimpleHandler)python_implementationz0.2) WSGIServerWSGIRequestHandlerdemo_app make_server WSGIServer// c@seZdZeZddZdS) ServerHandlerc Cs<z|j|jddd|jWt|dSt|w)Nr r)request_handler log_requeststatussplit bytes_sentrcloseselfr,/usr/lib/python3.10/wsgiref/simple_server.pyr s zServerHandler.closeN)__name__ __module__ __qualname__software_versionserver_softwarerrrrrr s r c@s4eZdZdZdZddZddZddZd d ZdS) rz7BaseHTTPServer that implements the Python WSGI protocolNcCst||dS)z.Override server_bind to store the server name.N)r server_bind setup_environrrrrr0s  zWSGIServer.server_bindcCsFi}|_|j|d<d|d<t|j|d<d|d<d|d<d|d<dS) N SERVER_NAMEzCGI/1.1GATEWAY_INTERFACE SERVER_PORT REMOTE_HOSTCONTENT_LENGTH SCRIPT_NAME) base_environ server_namestr server_port)renvrrrr5s   zWSGIServer.setup_environcCs|jSN applicationrrrrget_app?zWSGIServer.get_appcCs ||_dSr,r-)rr.rrrset_appBs zWSGIServer.set_app) rrr__doc__r.rrr/r1rrrrr*s rc@s,eZdZdeZddZddZddZdS) rr cCsL|jj}|j|d<|j|d<|j|d<d|jvr$|jdd\}}n|jd}}tj |d|d<||d <| }||j d krF||d <|j d |d <|j d dur]|j |d<n|j d |d<|j d}|rp||d<|j D].\}}|dd}|}||vrqud||vr|d|d|7<qu||d|<qu|S)NSERVER_PROTOCOLSERVER_SOFTWAREREQUEST_METHOD?rr#z iso-8859-1 PATH_INFO QUERY_STRINGrr$ REMOTE_ADDRz content-type CONTENT_TYPEzcontent-lengthr%-_HTTP_,)serverr'copyrequest_versionserver_versioncommandpathrurllibparseunquoteaddress_stringclient_addressheadersgetget_content_typeitemsreplaceupperstrip)rr+rDqueryhostlengthkvrrr get_environKs6        zWSGIRequestHandler.get_environcCstjSr,)sysstderrrrrr get_stderrpr0zWSGIRequestHandler.get_stderrcCs|jd|_t|jdkrd|_d|_d|_|ddS|s$dSt |j|j | | dd}||_ ||jdS)zHandle a single HTTP requestiir#iNF) multithread)rfilereadlineraw_requestlinelen requestlinerArC send_error parse_requestr wfilerYrVrrunr?r/)rhandlerrrrhandless zWSGIRequestHandler.handleN)rrr __version__rBrVrYrerrrrrGs % rcCsrddlm}|}td|dt|dt|}|D]\}}t|dt||dq|ddg|dgS) Nr)StringIOz Hello world!)file=z200 OK)z Content-Typeztext/plain; charset=utf-8zutf-8)iorgprintsortedrMreprgetvalueencode)environstart_responsergstdouthrTrUrrrrs      rcCs|||f|}|||S)zACreate a new WSGI server listening on `host` and `port` for `app`)r1)rRportapp server_class handler_classr?rrrr s r __main__r#i@zServing HTTP onrtrz...zhttp://localhost:8000/xyz?abc) r2 http.serverrrrW urllib.parserEwsgiref.handlersrplatformrrf__all__rBversionr sys_versionrr rrrr rhttpdsocket getsocknamesark webbrowseropenhandle_requestrrrrs4    C     "