
\[\!                 @   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 Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z d d	   Z e e d d
  Z e d k r}e d d e  Z e j j   Z e d e d d e d d  d d l Z e j d  e j   e j    n  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   sB   z- |  j  j |  j j d d  d |  j  Wd  t j |   Xd  S)Nr      r   )request_handlerZlog_requestZstatussplitZ
bytes_sentr   close)self r   +/usr/lib/python3.4/wsgiref/simple_server.pyr       s    	$zServerHandler.closeN)__name__
__module____qualname__software_versionZserver_softwarer   r   r   r   r   r      s   r   c               @   sL   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   sY   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   s  |  j  j j   } |  j | d <|  j | d <|  j | d <d |  j k rf |  j j d d  \ } } n |  j d } } t j	 j
 |  j d  | d <| | d	 <|  j   } | |  j d
 k r | | d <n  |  j d
 | d <|  j j d  d  k r
|  j j   | d <n |  j d | d <|  j j d  } | r@| | d <n  x |  j j   D]y \ } } | j d d  j   } | j   } | | k rqPn  d | | k r| d | d | 7<qP| | d | <qPW| 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unquote_to_bytesdecodeZ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 rV d |  _ d |  _ d |  _ |  j d  d S|  j   sf d St	 |  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<   r9   r   Z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   s   d d l  m } |   } t d d | t d |  t |  j    } x0 | D]( \ } } t | d t |  d | qO 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)rD   rE   )iorA   printsortedr2   reprgetvalueencode)environZstart_responserA   stdouthr7   r8   r   r   r   r      s    	 c             C   s&   | |  | f |  } | j  |  | S)zACreate a new WSGI server listening on `host` and `port` for `app`)r"   )r6   portZappZserver_classZhandler_classr(   r   r   r   r	      s    __main__r   i@  zServing HTTP onrO   r   z...zhttp://localhost:8000/xyz?abc)!r#   Zhttp.serverr   r   r:   Zurllib.parser.   Zwsgiref.handlersr   platformr   r@   __all__r+   versionr   sys_versionr   r   r   r   r   r	   r   ZhttpdZsocketZgetsocknameZsarG   Z
webbrowseropenZhandle_requestZserver_closer   r   r   r   <module>   s.   
B
