???<!-- GIF89;a -->
123123123123
.....................................................................................................................................???<!-- GIF89;a -->
123123123123
.....................................................................................................................................3

  \                 @   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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j Zeded ded d ddlZejd ej  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| jjddd | j W d tj|  X d S )Nr      r   )request_handlerZlog_requestZstatussplitZ
bytes_sentr   close)self r   -/usr/lib64/python3.6/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 j|  | j  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 }| j|d< | j|d< | j|d< d| jkrH| jjdd\}}n| jd }}tj	j
|d|d< ||d	< | j }|| jd
 kr||d< | jd
 |d< | jjdd kr| jj |d< n| jd |d< | jjd}|r||d< xn| jj D ]`\}}|jddj }|j }||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hostZ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 jd| _t| jdkr<d| _d| _d| _| jd dS | j sHdS t	| j | j
| j | j }| |_|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;   r8   r   Zrunr(   r!   )r   Zhandlerr   r   r   handles   s    
zWSGIRequestHandler.handleN)r   r   r   __version__r+   r8   r;   r>   r   r   r   r   r   G   s   %c             C   sv   ddl m} | }td|d t|d t| j }x$|D ]\}}t|dt||d q:W |dd
g |j jd	gS )Nr   )StringIOzHello world!)file=z200 OKContent-Typetext/plain; charset=utf-8zutf-8)rC   rD   )ior@   printsortedr1   reprgetvalueencode)environZstart_responser@   stdouthr6   r7   r   r   r   r      s    
c             C   s   || |f|}|j | |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   r9   Zurllib.parser.   Zwsgiref.handlersr   platformr   r?   __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


