3 \ã @s dZddlmZmZddlZddlZddlmZddl m Z dZ ddd d gZ d e Z e ƒd ejjƒdZe d eZGdd„deƒZGdd„deƒZGdd„deƒZdd „Zeefdd „Zedk�reddeƒ�BZejjƒZededdeddƒddlZejdƒejƒWdQRXdS)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Ú WSGIServerÚWSGIRequestHandlerÚdemo_appÚ make_serverz WSGIServer/ú/ú c@seZdZeZdd„ZdS)Ú ServerHandlerc Cs4z"|jj|jjddƒd|jƒWdtj|ƒXdS)Nr ér)Úrequest_handlerZ log_requestZstatusÚsplitZ bytes_sentrÚclose)Úself©rú-/usr/lib64/python3.6/wsgiref/simple_server.pyr szServerHandler.closeN)Ú__name__Ú __module__Ú __qualname__Úsoftware_versionZserver_softwarerrrrrr sr c@s4eZdZdZdZdd„Zdd„Zdd„Zd d „ZdS) rz7BaseHTTPServer that implements the Python WSGI protocolNcCstj|ƒ|jƒdS)z.Override server_bind to store the server name.N)rÚ server_bindÚ setup_environ)rrrrr0s zWSGIServer.server_bindcCsFi}|_|j|d<d|d<t|jƒ|d<d|d<d|d<d|d<dS) NZ SERVER_NAMEzCGI/1.1ZGATEWAY_INTERFACEZ SERVER_PORTÚÚ REMOTE_HOSTÚCONTENT_LENGTHZ SCRIPT_NAME)Ú base_environZ server_nameÚstrZ server_port)rÚenvrrrr5s  zWSGIServer.setup_environcCs|jS)N)Ú application)rrrrÚget_app?szWSGIServer.get_appcCs ||_dS)N)r )rr rrrÚset_appBszWSGIServer.set_app) rrrÚ__doc__r rrr!r"rrrrr*s  c@s,eZdZdeZdd„Zdd„Zdd„ZdS) rz WSGIServer/cCsT|jjjƒ}|j|d<|j|d<|j|d<d|jkrH|jjddƒ\}}n |jd}}tj j |dƒ|d<||d <|j ƒ}||j d krŒ||d <|j d |d <|j jd ƒdkrº|j jƒ|d<n|j d |d<|j jdƒ}|rà||d<xn|j jƒD]`\}}|jddƒjƒ}|jƒ}||k�rqìd||k�r@|d|d|7<qì||d|<qìW|S)NZSERVER_PROTOCOLZSERVER_SOFTWAREZREQUEST_METHODú?r rz iso-8859-1Z PATH_INFOZ QUERY_STRINGrrZ REMOTE_ADDRz content-typeZ CONTENT_TYPEzcontent-lengthrú-Ú_ZHTTP_ú,)ÚserverrÚcopyÚrequest_versionÚserver_versionÚcommandÚpathrÚurllibÚparseZunquoteZaddress_stringZclient_addressZheadersÚgetZget_content_typeÚitemsÚreplaceÚupperÚstrip)rrr-ZqueryÚhostZlengthÚkÚvrrrÚ get_environKs8        zWSGIRequestHandler.get_environcCstjS)N)ÚsysÚstderr)rrrrÚ get_stderrpszWSGIRequestHandler.get_stderrcCs||jjdƒ|_t|jƒdkrrrrrrGs%cCsvddlm}|ƒ}td|d�t|d�t|jƒƒ}x$|D]\}}t|dt|ƒ|d�q:W|dd gƒ|jƒjd ƒgS) Nr)ÚStringIOz Hello world!)Úfileú=z200 OKú Content-Typeútext/plain; charset=utf-8zutf-8)rCrD)Úior@ÚprintÚsortedr1ÚreprÚgetvalueÚencode)ÚenvironZstart_responser@ÚstdoutÚhr6r7rrrr‰s     cCs|||f|ƒ}|j|ƒ|S)zACreate a new WSGI server listening on `host` and `port` for `app`)r")r5ÚportZappZ server_classZ handler_classr(rrrr •s Ú__main__ri@zServing HTTP onrNr z...zhttp://localhost:8000/xyz?abc) r#Z http.serverrrr9Z urllib.parser.Zwsgiref.handlersrÚplatformrr?Ú__all__r+ÚversionrÚ sys_versionrr rrrr rZhttpdZsocketZ getsocknameZsarFZ webbrowserÚopenZhandle_requestrrrrÚ s,    B