a
    If3                     @   s2  d Z ddlmZmZ ddlZddlZddlmZ ddl	m
Z
 dZg dZde Ze
 d	 ej d  Zed
 e ZG dd deZG dd deZG dd deZdd ZeefddZedkr.eddeLZej Zeded ded d ddlZed e  W d   n1 s$0    Y  dS )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_implementationz0.2)
WSGIServerWSGIRequestHandlerdemo_appmake_serverWSGIServer// c                   @   s   e Zd ZeZdd ZdS )ServerHandlerc              	   C   s>   z,| j | jddd | j W t|  nt|  0 d S )Nr      r   )request_handlerZlog_requestZstatussplitZ
bytes_sentr   closeself r   &lib/python3.9/wsgiref/simple_server.pyr       s
    zServerHandler.closeN)__name__
__module____qualname__software_versionZserver_softwarer   r   r   r   r   r      s   r   c                   @   s4   e Zd ZdZdZdd Zdd Zdd Zd	d
 ZdS )r   z7BaseHTTPServer that implements the Python WSGI protocolNc                 C   s   t |  |   dS )z.Override server_bind to store the server name.N)r   server_bindsetup_environr   r   r   r   r   0   s    
zWSGIServer.server_bindc                 C   sF   i  }| _ | j|d< d|d< t| j|d< d|d< d|d< d|d< d S )	NZSERVER_NAMEzCGI/1.1ZGATEWAY_INTERFACEZSERVER_PORT REMOTE_HOSTCONTENT_LENGTHZSCRIPT_NAME)base_environZserver_namestrZserver_port)r   envr   r   r   r   5   s    

zWSGIServer.setup_environc                 C   s   | j S Napplicationr   r   r   r   get_app?   s    zWSGIServer.get_appc                 C   s
   || _ d S r"   r#   )r   r$   r   r   r   set_appB   s    zWSGIServer.set_app)	r   r   r   __doc__r$   r   r   r%   r&   r   r   r   r   r   *   s   
r   c                   @   s,   e Zd Zde Zdd Zdd Zdd ZdS )	r   r
   c                 C   sP  | j j }| j|d< | j|d< | j|d< d| jv rH| jdd\}}n| jd }}tj	
|d|d< ||d	< |  }|| jd
 kr||d< | jd
 |d< | jdd u r| j |d< n| jd |d< | jd}|r||d< | j D ]`\}}|dd }| }||v rqd| |v r>|d|   d| 7  < q||d| < q|S )NZSERVER_PROTOCOLZSERVER_SOFTWAREZREQUEST_METHOD?r   r   z
iso-8859-1Z	PATH_INFOZQUERY_STRINGr   r   ZREMOTE_ADDRzcontent-typeZCONTENT_TYPEzcontent-lengthr   -_ZHTTP_,)serverr   copyrequest_versionserver_versioncommandpathr   urllibparseZunquoteZaddress_stringZclient_addressZheadersgetZget_content_typeitemsreplaceupperstrip)r   r!   r1   ZqueryhostZlengthkvr   r   r   get_environK   s6    




zWSGIRequestHandler.get_environc                 C   s   t jS r"   )sysstderrr   r   r   r   
get_stderrp   s    zWSGIRequestHandler.get_stderrc                 C   s   | j d| _t| jdkr<d| _d| _d| _| d dS |  sHdS t	| j | j
|  |  dd}| |_|| j  dS )zHandle a single HTTP requesti  i   r   i  NF)Zmultithread)ZrfilereadlineZraw_requestlinelenZrequestliner.   r0   Z
send_errorZparse_requestr   Zwfiler?   r<   r   runr,   r%   )r   Zhandlerr   r   r   handles   s    
zWSGIRequestHandler.handleN)r   r   r   __version__r/   r<   r?   rC   r   r   r   r   r   G   s   %r   c                 C   sr   ddl m} | }td|d t|d t|  }|D ]\}}t|dt||d q8|ddg | dgS )	Nr   )StringIOzHello world!)file=z200 OK)zContent-Typeztext/plain; charset=utf-8zutf-8)iorE   printsortedr5   reprgetvalueencode)environZstart_responserE   stdouthr:   r;   r   r   r   r      s    
r   c                 C   s   || |f|}| | |S )zACreate a new WSGI server listening on `host` and `port` for `app`)r&   )r9   portZappZserver_classZhandler_classr,   r   r   r   r	      s    
r	   __main__r   i@  zServing HTTP onrQ   r   z...zhttp://localhost:8000/xyz?abc) r'   Zhttp.serverr   r   r=   Zurllib.parser2   Zwsgiref.handlersr   platformr   rD   __all__r/   versionr   sys_versionr   r   r   r   r   r	   r   ZhttpdZsocketZgetsocknameZsarI   Z
webbrowseropenZhandle_requestr   r   r   r   <module>   s.   C
	


