B
     S]              	   @   s   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ddd	d
g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BZej Zeded ded d ddlZed e  W dQ R X 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_serverzWSGIServer// c               @   s   e Zd ZeZdd ZdS )ServerHandlerc          	   C   s4   z"| j | jddd | j W d t|  X d S )Nr      r   )request_handlerZlog_requestZstatussplitZ
bytes_sentr   close)self r   &lib/python3.7/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_environ)r   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)application)r   r   r   r   get_app?   s    zWSGIServer.get_appc             C   s
   || _ d S )N)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   
c               @   s,   e Zd Zde Zdd Zdd Zdd ZdS )	r   zWSGIServer/c             C   sT  | j j }| j|d< | j|d< | j|d< d| jkrH| jdd\}}n| jd }}tj	
|d|d< ||d	< |  }|| jd
 kr||d< | jd
 |d< | jdd kr| j |d< n| jd |d< | jd}|r||d< xn| j D ]`\}}|dd }| }||krqd| |kr@|d|   d| 7  < q||d| < qW |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   r-   Zqueryhostlengthkvr   r   r   get_environK   s8    



 
zWSGIRequestHandler.get_environc             C   s   t jS )N)sysstderr)r   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
|  |  }| |_|| j  dS )zHandle a single HTTP requesti  i   r   i  N)ZrfilereadlineZraw_requestlinelenZrequestliner*   r,   Z
send_errorZparse_requestr   Zwfiler<   r9   r   runr(   r!   )r   Zhandlerr   r   r   handles   s    
zWSGIRequestHandler.handleN)r   r   r   __version__r+   r9   r<   r@   r   r   r   r   r   G   s   %c             C   sv   ddl m} | }td|d t|d t|  }x$|D ]\}}t|dt||d q:W |ddg | dgS )	Nr   )StringIOzHello world!)file=z200 OK)zContent-Typeztext/plain; charset=utf-8zutf-8)iorB   printsortedr1   reprgetvalueencode)environZstart_responserB   stdouthr7   r8   r   r   r   r      s    
c             C   s   || |f|}| | |S )zACreate a new WSGI server listening on `host` and `port` for `app`)r"   )r5   portZappZserver_classZhandler_classr(   r   r   r   r	      s    
__main__r   i@  zServing HTTP onrN   r   z...zhttp://localhost:8000/xyz?abc) r#   Zhttp.serverr   r   r:   Zurllib.parser.   Zwsgiref.handlersr   platformr   rA   __all__r+   versionr   sys_versionr   r   r   r   r   r	   r   ZhttpdZsocketZgetsocknameZsarF   Z
webbrowseropenZhandle_requestr   r   r   r   <module>   s,   B


