grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026î ¤ feŒã@sRdZdZddlmZmZmZddlmZddlZddl Z ddl Z ddl m Z ddlmZddlmZddlZddlZddlZd d d d d dddddddddgZdadadd„Zdd„Zdd„Zdd„Zeadade jddd d „Zddd!d „Z ddd"d „Z!d#d„Z"d$d%„Z#d&d„Z$Gd'd „d ƒZ%Gd(d „d ƒZ&e jd)d*„Z'ddddd+d„Z(e jd,d„Z)d-d„Z*d.d„Z+d/d„Z,d0d„Z-dd1d„Z.d2d3„Z/e0d4krNe'ƒndS)5z�Support module for CGI (Common Gateway Interface) scripts. This module defines a number of utilities for use by CGI scripts written in Python. z2.6é)ÚStringIOÚBytesIOÚ TextIOWrapper)ÚMappingN)Ú FeedParser)ÚMessage)ÚwarnÚMiniFieldStorageÚ FieldStorageÚparseÚparse_qsÚ parse_qslÚparse_multipartÚ parse_headerÚprint_exceptionÚ print_environÚ print_formÚprint_directoryÚprint_argumentsÚprint_environ_usageÚescapeÚc Gs[tr8t r8yttdƒaWq8tk r4Yq8XntsGtantat|ŒdS)a„Write a log message, if there is a log file. Even though this function is called initlog(), you should always use log(); log is a variable that is set either to initlog (initially), to dolog (once the log file has been opened), or to nolog (when logging is disabled). The first argument is a format string; the remaining arguments (if any) are arguments to the % operator, so e.g. log("%s: %s", "a", "b") will write "a: b" to the log file, followed by a newline. If the global logfp is not None, it should be a file object to which log data is written. If the global logfp is None, the global logfile may be a string giving a filename to open, in append mode. This file should be world writable!!! If the file can't be opened, logging is silently disabled (since there is no safe place where we could send an error message). ÚaN)ÚlogfileÚlogfpÚopenÚOSErrorÚnologÚlogÚdolog)Úallargs©r!ú(/opt/alt/python34/lib64/python3.4/cgi.pyÚinitlog:s   r#cGstj||dƒdS)z=Write a log message to the log file. See initlog() for docs.Ú N)rÚwrite)ZfmtÚargsr!r!r"r]srcGsdS)z9Dummy function, assigned to log when logging is disabled.Nr!)r r!r!r"rasrcCs)datrtjƒdantadS)zClose the log file.rN)rrÚcloser#rr!r!r!r"Úcloseloges   r(c Csé|dkrtj}nt|dƒr3|j}nd}t|tƒrT|j}nd|krmd|drCZhttprIZnextpartZlastpartZpartdictZ terminatorÚbytesÚdataÚheadersrDÚlinesÚlineÚkeyZparamsrJr!r!r"rÉs|                   ccsÛxÔ|dd…dkrÖ|dd…}|jdƒ}xR|dkrŒ|jdd|ƒ|jdd|ƒdrŒ|jd|dƒ}q;W|dkr¨t|ƒ}n|d|…}|jƒV||d…}qWdS)Nr1ú;rú"z\"rF)ÚfindÚcountÚlenÚstrip)ÚsÚendÚfr!r!r"Ú _parseparam-s;  rccCstd|ƒ}|jƒ}i}xÓ|D]Ë}|jdƒ}|dkr)|d|…jƒjƒ}||dd…jƒ}t|ƒdkrç|d|d ko±dknrç|dd …}|jdd ƒjd dƒ}n|||| d krNdi}} n d i}} ||_| |_"d| kr•| dj|jƒ|_#n d|_#d}d|jkryt$|jdƒ}Wnt%k rÞYnXt&r|t&krt%dƒ‚qn||_'|jdkr-|r-||_nd|_(|_)d|_*|d kr_|j+ƒn6|dd…dkr‹|j,|||ƒn |j-ƒdS)a›Constructor. Read multipart/* until last part. Arguments, all optional: fp : file pointer; default: sys.stdin.buffer (not used when the request method is GET) Can be : 1. a TextIOWrapper object 2. an object whose read() and readline() methods return bytes headers : header dictionary-like object; default: taken from environ as per CGI spec outerboundary : terminating multipart boundary (for internal use only) environ : environment dictionary; default: os.environ keep_blank_values: flag indicating whether blank values in percent-encoded forms should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included. strict_parsing: flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception. limit : used internally to read parts of multipart/form-data forms, to exit from the reading loop when reached. It is the difference between the form content-length and the number of bytes already read encoding, errors : the encoding and error handler used to decode the binary stream to strings. Must be the same as the charset defined for the page sending the form (content-type : meta http-equiv or header) r+r*NZHEADr/r1rÚsurrogateescapez!application/x-www-form-urlencodedz content-typer,r-r.zcontent-lengthz?headers must be mapping or an instance of email.message.Messager:rOzfp must be file pointerz#outerboundary must be bytes, not %srzcontent-dispositionrJrtz text/plainrIrHzMaximum content length exceededé z multipart/rK).r@rAÚupperÚ qs_on_postr2r<ÚencodeÚlocaleÚgetpreferredencodingrr5rrÚ TypeErrorrVr3r6r>rr4r)ÚerrorsrTrvrpÚ outerboundaryÚ bytes_readÚlimitrryrzrJrtÚ _binary_filerxÚ innerboundaryr7r9r8ÚlengthrurwÚdoneÚread_urlencodedÚ read_multiÚ read_single)rmr>rVr„r?r@rAr†r)rƒÚmethodrEZcdisprCrBZclenr!r!r"rn•sª+                                        zFieldStorage.__init__c Cs*y|jjƒWntk r%YnXdS)N)rwr'ÚAttributeError)rmr!r!r"Ú__del__3s zFieldStorage.__del__cCsd|j|j|jfS)z"Return a printable representation.zFieldStorage(%r, %r, %r))rJrtrl)rmr!r!r"ro9szFieldStorage.__repr__cCst|jƒƒS)N)ÚiterÚkeys)rmr!r!r"Ú__iter__>szFieldStorage.__iter__cCs{|dkrt|ƒ‚n|jrV|jjdƒ|jjƒ}|jjdƒn!|jdk rq|j}nd}|S)Nrlr)r�rwÚseekr:ru)rmrJrlr!r!r"Ú __getattr__As   zFieldStorage.__getattr__cCsŽ|jdkrtdƒ‚ng}x0|jD]%}|j|kr.|j|ƒq.q.W|slt|ƒ‚nt|ƒdkr†|dS|SdS)zDictionary style indexing.Nz not indexabler1r)rur‚rJrRÚKeyErrorr^)rmrYÚfoundÚitemr!r!r"Ú __getitem__NszFieldStorage.__getitem__cCsH||kr@||}t|tƒr6dd„|DƒS|jSn|SdS)z8Dictionary style get() method, including 'value' lookup.cSsg|]}|j‘qSr!)rl)Ú.0Úxr!r!r"ú as z)FieldStorage.getvalue..N)r5rurl)rmrYÚdefaultrlr!r!r"Úgetvalue\s    zFieldStorage.getvaluecCsB||kr:||}t|tƒr0|djS|jSn|SdS)z! Return the first value received.rN)r5rurl)rmrYr�rlr!r!r"Úgetfirstgs     zFieldStorage.getfirstcCsK||krC||}t|tƒr6dd„|DƒS|jgSngSdS)z Return list of received values.cSsg|]}|j‘qSr!)rl)ršr›r!r!r"rœws z(FieldStorage.getlist..N)r5rurl)rmrYrlr!r!r"Úgetlistrs    zFieldStorage.getlistcCs>|jdkrtdƒ‚nttdd„|jDƒƒƒS)zDictionary style keys() method.Nz not indexablecss|]}|jVqdS)N)rJ)ršr˜r!r!r"ú �sz$FieldStorage.keys..)rur‚Úset)rmr!r!r"r’}szFieldStorage.keyscs>|jdkrtdƒ‚nt‡fdd†|jDƒƒS)z%Dictionary style __contains__ method.Nz not indexablec3s|]}|jˆkVqdS)N)rJ)ršr˜)rYr!r"r¡‡sz,FieldStorage.__contains__..)rur‚Úany)rmrYr!)rYr"Ú __contains__ƒszFieldStorage.__contains__cCst|jƒƒS)z Dictionary style len(x) support.)r^r’)rmr!r!r"Ú__len__‰szFieldStorage.__len__cCs+|jdkrtdƒ‚nt|jƒS)NzCannot be converted to bool.)rur‚Úbool)rmr!r!r"Ú__bool__�szFieldStorage.__bool__cCsõ|jj|jƒ}t|tƒsItd|jt|ƒjfƒ‚n|j|j |j ƒ}|j r~|d|j 7}ng|_ t jj||j|jd|j d|j ƒ}x-|D]%\}}|j jt||ƒƒq¾W|jƒdS)z+Internal: read data in query string format.z%s should return bytes, got %sr0r)rƒN)r>r:r‰r5rTr9rvrpr;r)rƒr~rur=r r r@rArRr Ú skip_lines)rmrEÚqueryrYrlr!r!r"r‹’s   zFieldStorage.read_urlencodedc Cs“|j}t|ƒs+td|fƒ‚ng|_|jr£tjj|j|j|j d|j d|j ƒ}x0|D]%\}}|jj t ||ƒƒqwWn|jp²|j}|jjƒ} t| tƒsøtd|jt| ƒjfƒ‚n|jt| ƒ7_xG| jƒd|jkrV| rV|jjƒ} |jt| ƒ7_qWx+tƒ} d} x-|jjƒ} | | 7} | jƒslPqlqlW| s£Pn|jt| ƒ7_| j| j|j |j ƒƒ| jƒ} d| krù| d=n||j| |||||j|j|j |j ƒ }|j|j7_|jj |ƒ|js}|j|jkoxdknrZPqZqZW|j ƒd S) z/Internal: read a part that is itself multipart.z&Invalid boundary in multipart form: %rr)rƒz%s should return bytes, got %ss--rHzcontent-lengthrN)!rˆrMr9rur~r=r r r@rAr)rƒrRr ÚFieldStorageClassÚ __class__r>rOr5rTrvrpr…r^r_rZfeedr;r'r†rŠr‰r¨)rmr?r@rAZibr©rYrlÚklassZ first_lineÚparserZhdr_textrUrVÚpartr!r!r"rŒ¥sV            +zFieldStorage.read_multicCsD|jdkr&|jƒ|jƒn |jƒ|jjdƒdS)zInternal: read an atomic part.rN)r‰Ú read_binaryr¨Ú read_linesrwr”)rmr!r!r"r�Ûs    zFieldStorage.read_singleéicCsØ|jƒ|_|j}|dkrÔx­|dkrÐ|jjt||jƒƒ}t|tƒs…t d|jt |ƒj fƒ‚n|j t |ƒ7_ |s­d|_Pn|jj|ƒ|t |ƒ}q'WndS)zInternal: read binary data.rz%s should return bytes, got %sr1NrK)Ú make_filerwr‰r>r:ÚminÚbufsizer5rTr9rvrpr…r^rŠr%)rmZtodorUr!r!r"r¯æs   zFieldStorage.read_binarycCsV|jrtƒ|_|_ntƒ|_|_|jrH|jƒn |jƒdS)z0Internal: read lines until EOF or outerboundary.N)r‡rrwÚ_FieldStorage__filerr„Úread_lines_to_outerboundaryÚread_lines_to_eof)rmr!r!r"r°÷s    zFieldStorage.read_linescCs­|jdk rk|jjƒt|ƒdkrk|jƒ|_|jjƒ}|jj|ƒd|_qkn|jr‡|jj|ƒn"|jj|j|j |j ƒƒdS)z line is always bytes, not stringNiè) rµÚtellr^r²rwržr%r‡r;r)rƒ)rmrXrUr!r!r"Z__writes zFieldStorage.__writecCsRxK|jjdƒ}|jt|ƒ7_|s=d|_Pn|j|ƒqWdS)zInternal: read lines until EOF.r1éNirK)r>rOr…r^rŠÚ_FieldStorage__write)rmrXr!r!r"r·s zFieldStorage.read_lines_to_eofc Csªd|j}|d}d}d}d}xz||jkr?Pn|jjd ƒ}|jt|ƒ7_|t|ƒ7}|s‰d|_Pn|dkr¨||}d}n|jdƒrõ|rõ|jƒ}||krÙPn||krõd|_Pqõn|}|j dƒr)d}|d d…}d}nh|j d ƒrWd }|d d…}d}n:|j dƒr…d}|d d…}d }n d}d }|j ||ƒq,Wd S)zžInternal: read lines until outerboundary. Data is read as bytes: boundaries and line ends must be converted to bytes for comparisons. s--rHTrr1r¹s s NrFs FirKrLrKrK) r„r†r>rOr…r^rŠrPrQÚendswithrº) rmÚ next_boundaryÚ last_boundaryZdelimÚlast_line_lfendZ_readrXÚ strippedlineZodelimr!r!r"r¶sP             z(FieldStorage.read_lines_to_outerboundarycCsÕ|j s|jrdSd|j}|d}d}xš|jjdƒ}|jt|ƒ7_|sqd|_Pn|jdƒr¾|r¾|jƒ}||kr¢Pn||kr¾d|_Pq¾n|jdƒ}q7WdS) z5Internal: skip lines until outer boundary if defined.Ns--Tr1r¹s irK)r„rŠr>rOr…r^r»r_)rmr¼r½r¾rXr¿r!r!r"r¨Ls&       zFieldStorage.skip_linescCs6|jrtjdƒStjdd|jddƒSdS)a¯Overridable: return a readable & writable file. The file will be used as follows: - data is written to it - seek(0) - data is read from it The file is opened in binary mode for files, in text mode for other fields This version opens a temporary file for reading and writing, and immediately deletes (unlinks) it. The trick (on Unix!) is that the file can still be used, but it can't be opened by another process, and it will automatically be deleted when it is closed or when the current process terminates. If you want a more permanent file, you derive a class which overrides this method. If you want a visible temporary file that is nevertheless automatically deleted when the script terminates, try defining a __del__ method in a derived class which unlinks the temporary files you have created. zwb+zw+r)Únewliner$N)r‡ÚtempfileZ TemporaryFiler))rmr!r!r"r²bs   zFieldStorage.make_filei )rprqrrrsÚosr?rnr�ror“r•r™ržrŸr r’r¤r¥r§r‹rªrŒr�r´r¯r°rºr·r¶r¨r²r!r!r!r"r js8 *  œ           6    1 cCsætdƒtƒtjt_ybtƒ}tƒtƒt|ƒt|ƒt ƒdd„}|dd„}tdƒ|ƒWnt ƒYnXtdƒda y/tƒ}tƒtƒt|ƒt|ƒWnt ƒYnXd S) z—Robust test CGI script, usable as main program. Write minimal HTTP headers and dump all information provided to the script in HTML form. zContent-type: text/htmlcSstdƒdS)Nz,testing print_exception() -- italics?)Úexecr!r!r!r"rb•sztest..fcSs |ƒdS)Nr!)rbr!r!r"Úg—sztest..gz9

What follows is a test, not an actual exception:

z*

Second try with a small maxlen...

é2N) Úprintr2ÚstdoutÚstderrr rrrrrrr8)r?ÚformrbrÄr!r!r"Útest„s4            rÊcCs§|dkr$tjƒ\}}}nddl}tƒtdƒ|j||ƒ|j||ƒ}tdtjdj|dd…ƒƒtj|dƒfƒ~dS)Nrz+

Traceback (most recent call last):

z
%s%s
rr1rKrK) r2Úexc_infoÚ tracebackrÆÚ format_tbÚformat_exception_onlyÚhtmlrrS)rvrlÚtbr†rÌrur!r!r"r«s   cCs|t|jƒƒ}tƒtdƒtdƒx7|D]/}tdtj|ƒdtj||ƒƒq4WtdƒtƒdS)z#Dump the shell environment as HTML.z

Shell Environment:

z
z
z
z
N)Úsortedr’rÆrÏr)r?r’rYr!r!r"r¹s   - cCsÕt|jƒƒ}tƒtdƒ|s6tdƒntdƒx}|D]u}tdtj|ƒdddƒ||}tdtjtt|ƒƒƒd ƒtd tjt|ƒƒƒqGWtd ƒtƒd S) z$Dump the contents of a form as HTML.z

Form Contents:

z

No form fields.z

z
ú:raú zzz
z
N)rÑr’rÆrÏrÚreprrv)rÉr’rYrlr!r!r"rÄs    ! '! cCsƒtƒtdƒytjƒ}WnAtk rd}z!tdtjt|ƒƒƒWYdd}~XnXttj|ƒƒtƒdS)z#Dump the current directory as HTML.z#

Current Working Directory:

zOSError:N)rÆrÂÚgetcwdrrÏrÚstr)ÚpwdÚmsgr!r!r"rÔs /cCs0tƒtdƒtƒttjƒtƒdS)Nz

Command Line Arguments:

)rÆr2r<r!r!r!r"ràs   cCstdƒdS)z9Dump a list of environment variables used by CGI as HTML.aØ

These environment variables could have been set:

  • AUTH_TYPE
  • CONTENT_LENGTH
  • CONTENT_TYPE
  • DATE_GMT
  • DATE_LOCAL
  • DOCUMENT_NAME
  • DOCUMENT_ROOT
  • DOCUMENT_URI
  • GATEWAY_INTERFACE
  • LAST_MODIFIED
  • PATH
  • PATH_INFO
  • PATH_TRANSLATED
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_HOST
  • REMOTE_IDENT
  • REMOTE_USER
  • REQUEST_METHOD
  • SCRIPT_NAME
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_ROOT
  • SERVER_SOFTWARE
In addition, HTTP headers sent by the server may be passed in the environment as well. Here are some common variable names:
  • HTTP_ACCEPT
  • HTTP_CONNECTION
  • HTTP_HOST
  • HTTP_PRAGMA
  • HTTP_REFERER
  • HTTP_USER_AGENT
N)rÆr!r!r!r"rçs'cCshtdtddƒ|jddƒ}|jddƒ}|jdd ƒ}|rd|jd d ƒ}n|S) zDeprecated API.z1cgi.escape is deprecated, use html.escape insteadÚ stacklevelrFr0z&úz>r[z")rrGrh)r`Zquoter!r!r"rs cCs:ddl}t|tƒr$d}nd}|j||ƒS)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)Úrer5rTÚmatch)r`rÜZ _vb_patternr!r!r"rM"s   rMÚ__main__)1rsÚ __version__ÚiorrrÚ collectionsrr2rÂZ urllib.parser=Z email.parserrZ email.messagerÚwarningsrrÏr€rÁÚ__all__rrr#rrr(rr8r?r r r rrcrr r rÊrrrrrrrrMrpr!r!r!r"Ús\            #   E d ÿÿ'   /