B °-_óã@sâdZdZdddddgZddlZddlZddlZddlZddlZ ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZddlZddlZddlZddlZdd lmZdd l mZd Zd ZGd d„dejƒZGdd„dejeƒZGdd„dejƒZ Gdd„de ƒZ!dd„Z"da#dd„Z$dd„Z%Gdd„de!ƒZ&e edddfdd„Z'e(dkrÞddl)Z)e) *¡Z+e+j,ddd d!e+j,d"d#dd$d%d&e+j,d'd(e  -¡d)d*e+j,d+d,de.d-d.d/e+ /¡Z0e0j1r¾e&Z2nee!e0j3d0Z2e'e2e0j4e0j5d1dS)2a@HTTP server classes. Note: BaseHTTPRequestHandler doesn't implement any HTTP request; see SimpleHTTPRequestHandler for simple implementations of GET, HEAD and POST, and CGIHTTPRequestHandler for CGI scripts. It does, however, optionally implement HTTP/1.1 persistent connections, as of version 0.3. Notes on CGIHTTPRequestHandler ------------------------------ This class implements GET and POST requests to cgi-bin scripts. If the os.fork() function is not present (e.g. on Windows), subprocess.Popen() is used as a fallback, with slightly altered semantics. In all cases, the implementation is intentionally naive -- all requests are executed synchronously. SECURITY WARNING: DON'T USE THIS CODE UNLESS YOU ARE INSIDE A FIREWALL -- it may execute arbitrary Python code or external programs. Note that status code 200 is sent prior to execution of a CGI script, so scripts cannot send other status codes such as 302 (redirect). XXX To do: - log requests even later (to capture byte count) - log user-agent header and other interesting goodies - send error log to separate file z0.6Ú HTTPServerÚThreadingHTTPServerÚBaseHTTPRequestHandlerÚSimpleHTTPRequestHandlerÚCGIHTTPRequestHandleréN)Úpartial)Ú HTTPStatusa¹ Error response

Error response

Error code: %(code)d

Message: %(message)s.

Error code explanation: %(code)s - %(explain)s.

ztext/html;charset=utf-8c@seZdZdZdd„ZdS)récCs4tj |¡|jdd…\}}t |¡|_||_dS)z.Override server_bind to store the server name.Né)Ú socketserverÚ TCPServerÚ server_bindÚserver_addressÚsocketZgetfqdnÚ server_nameÚ server_port)ÚselfÚhostÚport©rú!/usr/lib/python3.7/http/server.pyr ‡s  zHTTPServer.server_bindN)Ú__name__Ú __module__Ú __qualname__Zallow_reuse_addressr rrrrrƒsc@seZdZdZdS)rTN)rrrZdaemon_threadsrrrrrsc @seZdZdZdej ¡dZdeZ e Z e Z dZdd„Zdd „Zd d „Zd d „Zd@dd„ZdAdd„ZdBdd„Zdd„Zdd„Zdd„ZdCdd„Zdd„Zd d!„Zd"d#„ZdDd$d%„Zd&d'„Zd(d)d*d+d,d-d.gZdd/d0d1d2d3d4d5d6d7d8d9d:g Z d;d<„Z!d=Z"e#j$j%Z&d>d?„e'j( )¡DƒZ*dS)Era¿HTTP request handler base class. The following explanation of HTTP serves to guide you through the code as well as to expose any misunderstandings I may have about HTTP (so you don't need to read the code to figure out I'm wrong :-). HTTP (HyperText Transfer Protocol) is an extensible protocol on top of a reliable stream transport (e.g. TCP/IP). The protocol recognizes three parts to a request: 1. One line identifying the request type and path 2. An optional set of RFC-822-style headers 3. An optional data part The headers and data are separated by a blank line. The first line of the request has the form where is a (case-sensitive) keyword such as GET or POST, is a string containing path information for the request, and should be the string "HTTP/1.0" or "HTTP/1.1". is encoded using the URL encoding scheme (using %xx to signify the ASCII character with hex code xx). The specification specifies that lines are separated by CRLF but for compatibility with the widest range of clients recommends servers also handle LF. Similarly, whitespace in the request line is treated sensibly (allowing multiple spaces between components and allowing trailing whitespace). Similarly, for output, lines ought to be separated by CRLF pairs but most clients grok LF characters just fine. If the first line of the request has the form (i.e. is left out) then this is assumed to be an HTTP 0.9 request; this form has no optional headers and data part and the reply consists of just the data. The reply form of the HTTP 1.x protocol again has three parts: 1. One line giving the response code 2. An optional set of RFC-822-style headers 3. The data Again, the headers and data are separated by a blank line. The response code line has the form where is the protocol version ("HTTP/1.0" or "HTTP/1.1"), is a 3-digit response code indicating success or failure of the request, and is an optional human-readable string explaining what the response code means. This server parses the request and the headers, and then calls a function specific to the request type (). Specifically, a request SPAM will be handled by a method do_SPAM(). If no such method exists the server sends an error response to the client. If it exists, it is called with no arguments: do_SPAM() Note that the request name is case sensitive (i.e. SPAM and spam are different requests). The various request details are stored in instance variables: - client_address is the client IP address in the form (host, port); - command, path and version are the broken-down request line; - headers is an instance of email.message.Message (or a derived class) containing the header information; - rfile is a file object open for reading positioned at the start of the optional input data part; - wfile is a file object open for writing. IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING! The first thing to be written must be the response line. Then follow 0 or more header lines, then a blank line, and then the actual data (if any). The meaning of the header lines depends on the command executed by the server; in most cases, when data is returned, there should be at least one header line of the form Content-type: / where and should be registered MIME types, e.g. "text/html" or "text/plain". zPython/rz BaseHTTP/zHTTP/0.9c CsÂd|_|j|_}d|_t|jdƒ}| d¡}||_| ¡}t |ƒdkrLdSt |ƒdkr |d}yT|  d ¡srt ‚| d d ¡d }| d ¡}t |ƒd krœt ‚t |dƒt |d ƒf}Wn*t t fk rà| tjd|¡dSX|dkrú|jdkrúd|_|dkr| tjd|¡dS||_d t |ƒkrrTÚversion_stringÚdate_time_string)rrMrNrrrrSãs  z$BaseHTTPRequestHandler.send_responsecCsd|jdkr`|dkr0||jkr,|j|d}nd}t|dƒs@g|_|j d|j||f dd¡¡dS) zSend the response header only.zHTTP/0.9NrrÚ_headers_bufferz %s %d %s zlatin-1Ústrict)r#rQrAr_Úappendr0rY)rrMrNrrrr>ðs   z)BaseHTTPRequestHandler.send_response_onlycCsl|jdkr6t|dƒsg|_|j d||f dd¡¡| ¡dkrh| ¡dkrVd|_n| ¡d krhd |_d S) z)Send a MIME header to the headers buffer.zHTTP/0.9r_z%s: %s zlatin-1r`Z connectionr Tz keep-aliveFN)r#rAr_rarYr8r$)rÚkeywordÚvaluerrrrTþs     z"BaseHTTPRequestHandler.send_headercCs"|jdkr|j d¡| ¡dS)z,Send the blank line ending the MIME headers.zHTTP/0.9s N)r#r_raÚ flush_headers)rrrrr? s  z"BaseHTTPRequestHandler.end_headerscCs(t|dƒr$|j d |j¡¡g|_dS)Nr_ó)rArDr[Újoinr_)rrrrrds z$BaseHTTPRequestHandler.flush_headersú-cCs.t|tƒr|j}| d|jt|ƒt|ƒ¡dS)zNLog an accepted request. This is called by send_response(). z "%s" %s %sN)Ú isinstancerrcÚ log_messager(r%)rrMÚsizerrrr\s z"BaseHTTPRequestHandler.log_requestcGs|j|f|žŽdS)zÿLog an error. This is called when a request cannot be fulfilled. By default it passes the message on to log_message(). Arguments are the same as for log_message(). XXX This should go to the separate error log. N)ri)rÚformatÚargsrrrrF"s z BaseHTTPRequestHandler.log_errorcGs&tj d| ¡| ¡||f¡dS)aëLog an arbitrary message. This is used by all other logging functions. Override it if you have specific logging wishes. The first argument, FORMAT, is a format string for the message to be logged. If the format string contains any % escapes requiring parameters, they should be specified as subsequent arguments (it's just like printf!). The client ip and current date/time are prefixed to every message. z%s - - [%s] %s N)ÚsysÚstderrr[Úaddress_stringÚlog_date_time_string)rrkrlrrrri0sz"BaseHTTPRequestHandler.log_messagecCs|jd|jS)z*Return the server software version string.ú )Úserver_versionÚ sys_version)rrrrr]Fsz%BaseHTTPRequestHandler.version_stringcCs |dkrt ¡}tjj|ddS)z@Return the current date and time formatted for a message header.NT)Zusegmt)ÚtimeÚemailÚutilsZ formatdate)rZ timestamprrrr^Jsz'BaseHTTPRequestHandler.date_time_stringc CsBt ¡}t |¡\ }}}}}}}} } d||j|||||f} | S)z.Return the current time formatted for logging.z%02d/%3s/%04d %02d:%02d:%02d)rtZ localtimeÚ monthname) rZnowZyearZmonthZdayZhhZmmZssÚxÚyÚzÚsrrrrpPs z+BaseHTTPRequestHandler.log_date_time_stringZMonZTueZWedZThuZFriZSatZSunZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDeccCs |jdS)zReturn the client address.r)Úclient_address)rrrrro^sz%BaseHTTPRequestHandler.address_stringzHTTP/1.0cCsi|]}|j|jf|“qSr)ÚphraseZ description)Ú.0Úvrrrú msz!BaseHTTPRequestHandler.)NN)N)N)rgrg)N)+rrrÚ__doc__rmr:r)rsÚ __version__rrÚDEFAULT_ERROR_MESSAGErVÚDEFAULT_ERROR_CONTENT_TYPErZr"r=r9rIrJr/rSr>rTr?rdr\rFrir]r^rpZ weekdaynamerwror0r2r3Z HTTPMessager5rÚ __members__ÚvaluesrQrrrrr“s>fc% 5     cs’eZdZdZdeZddœ‡fdd„ Zdd„Zd d „Zd d „Z d d„Z dd„Z dd„Z dd„Z ejsle ¡ej ¡Ze dddddœ¡‡ZS)raWSimple HTTP request handler with GET and HEAD commands. This serves files from the current directory and any of its subdirectories. The MIME type for files is determined by calling the .guess_type() method. The GET and HEAD requests are identical except that the HEAD request omits the actual contents of the file. z SimpleHTTP/N)Ú directorycs(|dkrt ¡}||_tƒj||ŽdS)N)ÚosÚgetcwdr‡ÚsuperÚ__init__)rr‡rlÚkwargs)Ú __class__rrr‹‚sz!SimpleHTTPRequestHandler.__init__cCs.| ¡}|r*z| ||j¡Wd| ¡XdS)zServe a GET request.N)Ú send_headÚcopyfilerDr )rÚfrrrÚdo_GETˆs zSimpleHTTPRequestHandler.do_GETcCs| ¡}|r| ¡dS)zServe a HEAD request.N)rŽr )rrrrrÚdo_HEAD‘sz SimpleHTTPRequestHandler.do_HEADc Cs>| |j¡}d}tj |¡rÄtj |j¡}|j d¡sŒ| t j ¡|d|d|dd|d|df}tj  |¡}|  d|¡|  ¡dSx6d D]$}tj ||¡}tj |¡r’|}Pq’W| |¡S| |¡}yt|d ƒ}Wn$tk r| t jd ¡dSXyt | ¡¡}d |jkrØd |jkrØytj |jd ¡} Wnttttfk r`YnxX| j dkr~| j!t"j#j$d} | j t"j#j$krØt"j" %|j&t"j#j$¡} | j!dd} | | krØ| t j'¡|  ¡| (¡dS| t j)¡|  d|¡|  dt*|dƒ¡|  d| +|j&¡¡|  ¡|S| (¡‚YnXdS)a{Common code for GET and HEAD commands. This sends the response code and MIME headers. Return value is either a file object (which has to be copied to the outputfile by the caller unless the command was HEAD, and must be closed by the caller under all circumstances), or None, in which case the caller has nothing further to do. Nrrr r réZLocation)z index.htmlz index.htmÚrbzFile not foundzIf-Modified-Sincez If-None-Match)Útzinfo)Z microsecondz Content-typezContent-Lengthéz Last-Modified),Útranslate_pathr1rˆÚisdirÚurllibÚparseZurlsplitÚendswithrSrZMOVED_PERMANENTLYZ urlunsplitrTr?rfÚexistsÚlist_directoryÚ guess_typeÚopenÚOSErrorr/Ú NOT_FOUNDÚfstatÚfilenor6rurvZparsedate_to_datetimeÚ TypeErrorr.Ú OverflowErrorr,r•rPÚdatetimeZtimezoneZutcZ fromtimestampÚst_mtimerUr ÚOKr%r^) rr1rÚpartsZ new_partsZnew_urlÚindexZctypeZfsZimsZ last_modifrrrrŽ—sl                  z"SimpleHTTPRequestHandler.send_headc CsØyt |¡}Wn"tk r0| tjd¡dSX|jdd„dg}ytjj |j dd}Wn t k r|tj  |¡}YnXt j |dd }t ¡}d |}| d ¡| d ¡| d |¡| d|¡| d|¡| d¡x~|D]v}tj  ||¡}|} } tj  |¡r"|d} |d} tj  |¡r8|d} | dtjj| ddt j | dd f¡qêW| d¡d |¡ |d¡} t ¡} |  | ¡|  d¡| tj¡| dd|¡| dtt| ƒƒ¡| ¡| S)zûHelper to produce a directory listing (absent index.html). Return value is either a file object, or None (indicating an error). In either case, the headers are sent, making the interface the same as for send_head(). zNo permission to list directoryNcSs| ¡S)N)r8)ÚarrrÚõrez9SimpleHTTPRequestHandler.list_directory..)ÚkeyÚ surrogatepass)ÚerrorsF)rLzDirectory listing for %szZz z@z%s z

%s

z
    rú@z
  • %s
  • z

Ú Úsurrogateescaperz Content-typeztext/html; charset=%szContent-Length) rˆÚlistdirr r/rr¡Úsortr™ršÚunquoter1ÚUnicodeDecodeErrorrWrXrmÚgetfilesystemencodingrarfr˜ÚislinkrLrYÚioÚBytesIOr[ÚseekrSr¨rTr%r*r?) rr1ÚlistÚrZ displaypathÚencÚtitleÚnameÚfullnameZ displaynameZlinknameZencodedrrrrræs\          z'SimpleHTTPRequestHandler.list_directorycCsÒ| dd¡d}| dd¡d}| ¡ d¡}ytjj|dd}Wn tk rbtj |¡}YnXt |¡}| d¡}t d|ƒ}|j }x8|D]0}t j   |¡sŽ|t jt jfkr°qŽt j  ||¡}qŽW|rÎ|d7}|S) zñTranslate a /-separated PATH to the local filename syntax. Components that mean special things to the local file system (e.g. drive or directory names) are ignored. (XXX They should probably be diagnosed.) ú?r rú#rr®)r¯N)r)r'r›r™ršrµr¶Ú posixpathÚnormpathÚfilterr‡rˆr1ÚdirnameÚcurdirÚpardirrf)rr1Ztrailing_slashr;Zwordrrrr— s$     z'SimpleHTTPRequestHandler.translate_pathcCst ||¡dS)añCopy all data between two file objects. The SOURCE argument is a file object open for reading (or anything with a read() method) and the DESTINATION argument is a file object open for writing (or anything with a write() method). The only reason for overriding this would be to change the block size or perhaps to replace newlines by CRLF -- note however that this the default server uses this to copy binary data as well. N)ÚshutilZ copyfileobj)rÚsourceZ outputfilerrrr>sz!SimpleHTTPRequestHandler.copyfilecCsLt |¡\}}||jkr"|j|S| ¡}||jkr>|j|S|jdSdS)a»Guess the type of a file. Argument is a PATH (a filename). Return value is a string of the form type/subtype, usable for a MIME Content-type header. The default implementation looks the file's extension up in the table self.extensions_map, using application/octet-stream as a default; however it would be permissible (if slow) to look inside the data to make a better guess. rN)rÄÚsplitextÚextensions_mapr8)rr1ÚbaseZextrrrržNs    z#SimpleHTTPRequestHandler.guess_typezapplication/octet-streamz text/plain)rz.pyz.cz.h)rrrrr‚rrr‹r‘r’rŽrr—rržÚ mimetypesZinitedZinitZ types_mapÚcopyrÍÚupdateÚ __classcell__rr)rrrss$  O: c CsÒ| d¡\}}}tj |¡}| d¡}g}x<|dd…D],}|dkrN| ¡q8|r8|dkr8| |¡q8W|rœ| ¡}|r |dkrŽ| ¡d}q |dkr d}nd}|r²d ||f¡}dd |¡|f}d |¡}|S)aÌ Given a URL path, remove extra '/'s and '.' path elements and collapse any '..' references and returns a collapsed path. Implements something akin to RFC-2396 5.2 step 6 to parse relative paths. The utility of this function is limited to is_cgi method and helps preventing some security attacks. Returns: The reconstituted URL, which will always start with a '/'. Raises: IndexError if too many '..' occur within the path. rÂrNrz..rr)Ú partitionr™ršrµr)Úpoprarf) r1Ú_ÚqueryÚ path_partsZ head_partsÚpartZ tail_partZ splitpathÚcollapsed_pathrrrÚ_url_collapse_pathss.     rÚcCsptrtSy ddl}Wntk r(dSXy| d¡daWn.tk rjdtdd„| ¡DƒƒaYnXtS) z$Internal routine to get nobody's uidrNrÚnobodyr r css|]}|dVqdS)r Nr)r~rxrrrú ±sznobody_uid..)rÛÚpwdÚ ImportErrorÚgetpwnamrRÚmaxZgetpwall)rÝrrrÚ nobody_uid¥s  rácCst |tj¡S)zTest for executable file.)rˆÚaccessÚX_OK)r1rrrÚ executableµsräc@sVeZdZdZeedƒZdZdd„Zdd„Z dd „Z d d gZ d d „Z dd„Z dd„ZdS)rz­Complete HTTP server with GET, HEAD and POST commands. GET and HEAD also support running CGI scripts. The POST command is *only* implemented for CGI scripts. ÚforkrcCs$| ¡r| ¡n| tjd¡dS)zRServe a POST request. This is only implemented for CGI scripts. zCan only POST to CGI scriptsN)Úis_cgiÚrun_cgir/rrB)rrrrÚdo_POSTËs  zCGIHTTPRequestHandler.do_POSTcCs| ¡r| ¡St |¡SdS)z-Version of send_head that support CGI scriptsN)rærçrrŽ)rrrrrŽÙszCGIHTTPRequestHandler.send_headcCsPt|jƒ}| dd¡}|d|…||dd…}}||jkrL||f|_dSdS)a3Test whether self.path corresponds to a CGI script. Returns True and updates the cgi_info attribute to the tuple (dir, rest) if self.path requires running a CGI script. Returns False otherwise. If any exception is raised, the caller should assume that self.path was rejected as invalid and act accordingly. The default implementation tests whether the normalized url path begins with one of the strings in self.cgi_directories (and the next character is a '/' or the end of the string). rr NTF)rÚr1ÚfindÚcgi_directoriesÚcgi_info)rrÙZdir_sepÚheadÚtailrrrræàs    zCGIHTTPRequestHandler.is_cgiz/cgi-binz/htbincCst|ƒS)z1Test whether argument path is an executable file.)rä)rr1rrrÚ is_executableúsz#CGIHTTPRequestHandler.is_executablecCstj |¡\}}| ¡dkS)z.Test whether argument path is a Python script.)z.pyz.pyw)rˆr1rÌr8)rr1rìrírrrÚ is_pythonþszCGIHTTPRequestHandler.is_pythonc) Cs¶|j\}}|d|}| dt|ƒd¡}x`|dkrŠ|d|…}||dd…}| |¡}tj |¡r†||}}| dt|ƒd¡}q,Pq,W| d¡\}}} | d¡}|dkrÊ|d|…||d…} }n |d} }|d| } | | ¡} tj | ¡s|  t j d| ¡dStj  | ¡s2|  t j d| ¡dS| | ¡} |jsJ| sl| | ¡sl|  t j d | ¡dSt tj¡}| ¡|d <|jj|d <d |d <|j|d<t|jjƒ|d<|j|d<tj |¡}||d<| |¡|d<| |d<| rô| |d<|jd|d<|j  d¡}|r¼| !¡}t|ƒdkr¼ddl"}ddl#}|d|d<|d $¡dkr¼y"|d %d¡}| &|¡ 'd¡}Wn|j(t)fk r–Yn&X| !d¡}t|ƒdkr¼|d|d<|j  d¡dkrÞ|j *¡|d<n|jd|d<|j  d¡}|r||d <|j  d!¡}|r ||d"<g}xN|j +d#¡D]>}|dd…d$krX| ,| -¡¡n||d%d… !d&¡}q2Wd& .|¡|d'<|j  d(¡}|rœ||d)<t/d|j 0d*g¡ƒ}d+ .|¡}|rÈ||d,<xd-D]}| 1|d¡qÎW| 2t j3d.¡| 4¡|  5d/d0¡}|jr,| g}d1|kr(| ,|¡t6ƒ}|j7 8¡t 9¡}|dkr¢t :|d¡\}}x0t; ;|jd2|¡dSy\yt ?|¡Wnt@k rÈYnXt A|j< B¡d¡t A|j7 B¡d¡t C| ||¡Wn(|j D|jE|j¡t Fd3¡YnXn†ddlG} | g}!| | ¡r€tHjI}"|" $¡ Jd4¡rt|"dd5…|"d6d…}"|"d7g|!}!d1| kr”|! ,| ¡| Kd8|  L|!¡¡y tM|ƒ}#WntNtOfk rÐd}#YnX| jP|!| jQ| jQ| jQ|d9}$|j $¡d:kr|#dkr|j< =|#¡}%nd}%x4t; ;|jd;|'¡|$jV W¡|$jX W¡|$jY}(|(r¨| >d2|(¡n | Kd<¡dS)=zExecute a CGI script.rr rNrÂrzNo such CGI script (%r)z#CGI script is not a plain file (%r)z!CGI script is not executable (%r)ZSERVER_SOFTWAREZ SERVER_NAMEzCGI/1.1ZGATEWAY_INTERFACEZSERVER_PROTOCOLZ SERVER_PORTZREQUEST_METHODZ PATH_INFOZPATH_TRANSLATEDZ SCRIPT_NAMEÚ QUERY_STRINGZ REMOTE_ADDRÚ authorizationr Z AUTH_TYPEZbasicÚasciiú:Z REMOTE_USERz content-typeZ CONTENT_TYPEzcontent-lengthÚCONTENT_LENGTHÚrefererÚ HTTP_REFERERÚacceptz éú,Z HTTP_ACCEPTz user-agentÚHTTP_USER_AGENTZcookiez, Ú HTTP_COOKIE)rðZ REMOTE_HOSTrôrúrûrözScript output followsú+rqú=zCGI script exit status %#xézw.exeéûÿÿÿéüÿÿÿz-uz command: %s)ÚstdinÚstdoutrnÚenvZpostz%szCGI script exited OK)Zrërér*r—rˆr1r˜rÓrœr/rr¡ÚisfileZ FORBIDDENrïÚ have_forkrîrÐZdeepcopyÚenvironr]Zserverrr0r%rr!r™ršrµr|r6r7r)Úbase64Úbinasciir8rYZ decodebytesÚdecodeÚErrorÚ UnicodeErrorZget_content_typeZgetallmatchingheadersraÚstriprfrÆZget_allÚ setdefaultrSr¨rdrPrárDrEråÚwaitpidÚselectr4ÚreadrFÚsetuidr Údup2r£ÚexecveZ handle_errorZrequestÚ_exitÚ subprocessrmrär›riZ list2cmdliner-r¤r,ÚPopenÚPIPEZ_sockZrecvZ communicater[rnr rÚ returncode))rÚdirÚrestr1ÚiZnextdirZnextrestZ scriptdirrÕrÖZscriptZ scriptnameZ scriptfileZispyrZuqrestrñrrÚlengthrõr÷ÚlineZuaÚcoZ cookie_strÚkZ decoded_queryrlrÛÚpidÚstsrZcmdlineZinterpÚnbytesÚpÚdatarrnZstatusrrrrçs2                                                 zCGIHTTPRequestHandler.run_cgiN)rrrrrArˆrZrbufsizerèrŽrærêrîrïrçrrrrrºs zHTTP/1.0i@rc Cs„||f}||_|||ƒb}|j ¡}d}t|j|d|ddƒy | ¡Wn&tk rttdƒt d¡YnXWdQRXdS)zmTest the HTTP request handler class. This runs an HTTP server on port 8000 (or the port argument). z>Serving HTTP on {host} port {port} (http://{host}:{port}/) ...rr )rrz& Keyboard interrupt received, exiting.N) r0rZ getsocknameÚprintrkZ serve_foreverÚKeyboardInterruptrmÚexit) Ú HandlerClassZ ServerClassZprotocolrÚbindrZhttpdZsaZ serve_messagerrrÚtestÃs   r*Ú__main__z--cgiÚ store_truezRun as CGI Server)ÚactionÚhelpz--bindz-bZADDRESSz8Specify alternate bind address [default: all interfaces])ÚdefaultÚmetavarr.z --directoryz-dz9Specify alternative directory [default:current directory])r/r.rZstorerÂz&Specify alternate port [default: 8000])r-r/ÚtypeÚnargsr.)r‡)r(rr))6rr‚Ú__all__rÐr¦Z email.utilsrurWZ http.clientr2r¹rÏrˆrÄrrÊrr rmrtZ urllib.parser™Ú functoolsrrrƒr„r rZThreadingMixInrZStreamRequestHandlerrrrÚrÛrárärr*rÚargparseÚArgumentParserÚparserÚ add_argumentr‰r-Ú parse_argsrlZcgiZ handler_classr‡rr)rrrrÚ sz3   c0