grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026 ”qcÎŒã@sOdZdZddlmZmZmZddlmZddlZddl 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/e0d4krKe'ƒdS)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 GsXtr5t r5yttdƒaWntk r4YnXtsDtantat|Œ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/python35/lib64/python3.5/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ƒdatadS)zClose the log file.rN)rrÚcloser#rr!r!r!r"Úcloseloges  r(c CsÔ|dkrtj}t|dƒr0|j}nd}t|tƒrN|j}d|krdd|d<|ddkrlt|dƒ\}}|dkr£t||ƒS|d krøt |d ƒ}t rÝ|t krÝt d ƒ‚|j |ƒj |ƒ}nd }d |kr+|r|d}||d }n4tjdd…r_|rN|d}|tjd}||d rCZhttprIZnextpartZlastpartZpartdictÚ 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|ƒ}|d|…}|jƒV||d…}qWdS)Nr1ú;rú"z\"rF)ÚfindÚcountÚlenÚstrip)ÚsÚendÚfr!r!r"Ú _parseparam-s;   rdcCsütd|ƒ}|jƒ}i}xÍ|D]Å}|jdƒ}|dkr)|d|…jƒjƒ}||dd…jƒ}t|ƒdkrä|d|d ko±dknrä|dd …}|jdd ƒjd dƒ}|||rr4r)ÚerrorsrUrwrqÚ outerboundaryÚ bytes_readÚlimitrrzr{rJruÚ _binary_fileryÚ innerboundaryr7r9r8ÚlengthrvrxÚdoneÚread_urlencodedÚ read_multiÚ read_single)rnr>rWr…r?r@rAr‡r)r„ÚmethodrEZcdisprCrBZclenr!r!r"ro•sª+                                               zFieldStorage.__init__c Cs*y|jjƒWntk r%YnXdS)N)rxr'ÚAttributeError)rnr!r!r"Ú__del__3s zFieldStorage.__del__cCs|S)Nr!)rnr!r!r"Ú __enter__9szFieldStorage.__enter__cGs|jjƒdS)N)rxr')rnr&r!r!r"Ú__exit__<szFieldStorage.__exit__cCsd|j|j|jfS)z"Return a printable representation.zFieldStorage(%r, %r, %r))rJrurm)rnr!r!r"rp?szFieldStorage.__repr__cCst|jƒƒS)N)ÚiterÚkeys)rnr!r!r"Ú__iter__DszFieldStorage.__iter__cCsx|dkrt|ƒ‚|jrS|jjdƒ|jjƒ}|jjdƒn!|jdk rn|j}nd}|S)Nrmr)r�rxÚseekr:rv)rnrJrmr!r!r"Ú __getattr__Gs    zFieldStorage.__getattr__cCs…|jdkrtdƒ‚g}x-|jD]"}|j|kr+|j|ƒq+W|sct|ƒ‚t|ƒdkr}|dS|SdS)zDictionary style indexing.Nz not indexabler1r)rvrƒrJrRÚKeyErrorr_)rnrZÚfoundÚitemr!r!r"Ú __getitem__Ts  zFieldStorage.__getitem__cCsH||kr@||}t|tƒr6dd„|DƒS|jSn|SdS)z8Dictionary style get() method, including 'value' lookup.cSsg|]}|j‘qSr!)rm)Ú.0Úxr!r!r"ú gs z)FieldStorage.getvalue..N)r5rvrm)rnrZÚdefaultrmr!r!r"Úgetvaluebs    zFieldStorage.getvaluecCsB||kr:||}t|tƒr0|djS|jSn|SdS)z! Return the first value received.rN)r5rvrm)rnrZr rmr!r!r"Úgetfirstms     zFieldStorage.getfirstcCsK||krC||}t|tƒr6dd„|DƒS|jgSngSdS)z Return list of received values.cSsg|]}|j‘qSr!)rm)r�ržr!r!r"rŸ}s z(FieldStorage.getlist..N)r5rvrm)rnrZrmr!r!r"Úgetlistxs    zFieldStorage.getlistcCs;|jdkrtdƒ‚ttdd„|jDƒƒƒS)zDictionary style keys() method.Nz not indexablecss|]}|jVqdS)N)rJ)r�r›r!r!r"ú ‡sz$FieldStorage.keys..)rvrƒÚset)rnr!r!r"r•ƒs zFieldStorage.keyscs;|jdkrtdƒ‚t‡fdd†|jDƒƒS)z%Dictionary style __contains__ method.Nz not indexablec3s|]}|jˆkVqdS)N)rJ)r�r›)rZr!r"r¤�sz,FieldStorage.__contains__..)rvrƒÚany)rnrZr!)rZr"Ú __contains__‰s zFieldStorage.__contains__cCst|jƒƒS)z Dictionary style len(x) support.)r_r•)rnr!r!r"Ú__len__�szFieldStorage.__len__cCs(|jdkrtdƒ‚t|jƒS)NzCannot be converted to bool.)rvrƒÚbool)rnr!r!r"Ú__bool__“s zFieldStorage.__bool__cCsï|jj|jƒ}t|tƒsFtd|jt|ƒjfƒ‚|j|j |j ƒ}|j rx|d|j 7}g|_ 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Šr5rUr9rwrqr;r)r„rrvr=r r r@rArRr Ú skip_lines)rnrEÚqueryrZrmr!r!r"rŒ˜s   zFieldStorage.read_urlencodedc Cs~|j}t|ƒs(td|fƒ‚g|_|jr�tjj|j|j|j d|j d|j ƒ}x-|D]%\}}|jj t ||ƒƒqtW|jp¬|j}|jjƒ} t| tƒsïtd|jt| ƒjfƒ‚|jt| ƒ7_xG| jƒd|jkrM| rM|jjƒ} |jt| ƒ7_qWxtƒ} d} x*|jjƒ} | | 7} | jƒscPqcW| s”P|jt| ƒ7_| j| j|j |j ƒƒ| jƒ} d| krç| d=||j| |||||j|j|j |j ƒ }|j|j7_|jj |ƒ|jsk|j|jkofdknrQPqQW|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‰rMr9rvrr=r r r@rAr)r„rRr ÚFieldStorageClassÚ __class__r>rOr5rUrwrqr†r_r`rZfeedr;r'r‡r‹rŠr«)rnr?r@rAZibr¬rZrmÚklassZ first_lineÚparserZhdr_textrVrWÚ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_linesrxr—)rnr!r!r"rŽás    zFieldStorage.read_singleéicCsÏ|jƒ|_|j}|dkrËx¤|dkrÊ|jjt||jƒƒ}t|tƒs‚t d|jt |ƒj fƒ‚|j t |ƒ7_ |s§d|_P|jj|ƒ|t |ƒ}q'WdS)zInternal: read binary data.rz%s should return bytes, got %sr1NrK)Ú make_filerxrŠr>r:ÚminÚbufsizer5rUr9rwrqr†r_r‹r%)rnZtodorVr!r!r"r²ìs   zFieldStorage.read_binarycCsV|jrtƒ|_|_ntƒ|_|_|jrH|jƒn |jƒdS)z0Internal: read lines until EOF or outerboundary.N)rˆrrxÚ_FieldStorage__filerr…Úread_lines_to_outerboundaryÚread_lines_to_eof)rnr!r!r"r³ýs    zFieldStorage.read_linescCs§|jdk re|jjƒt|ƒdkre|jƒ|_|jjƒ}|jj|ƒd|_|jr�|jj|ƒn"|jj|j|j |j ƒƒdS)z line is always bytes, not stringNiè) r¸Útellr_rµrxr¡r%rˆr;r)r„)rnrYrVr!r!r"Z__writes  zFieldStorage.__writecCsOxH|jjdƒ}|jt|ƒ7_|s:d|_P|j|ƒqWdS)zInternal: read lines until EOF.r1éNirK)r>rOr†r_r‹Ú_FieldStorage__write)rnrYr!r!r"rºs zFieldStorage.read_lines_to_eofc Cs˜d|j}|d}d}d}d}xh||jkr<P|jjd ƒ}|jt|ƒ7_|t|ƒ7}|sƒd|_P|dkrŸ||}d}|jdƒrã|rã|jƒ}||krÍP||krãd|_P|}|j dƒrd}|d d…}d}nh|j d ƒrEd }|d d…}d}n:|j dƒrsd}|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½) rnÚ next_boundaryÚ last_boundaryZdelimÚlast_line_lfendZ_readrYÚ strippedlineZodelimr!r!r"r¹!sP            z(FieldStorage.read_lines_to_outerboundarycCsÉ|j s|jrdSd|j}|d}d}xŽ|jjdƒ}|jt|ƒ7_|snd|_P|jdƒr²|r²|jƒ}||krœP||kr²d|_P|jdƒ}q7WdS) z5Internal: skip lines until outer boundary if defined.Ns--Tr1r¼s irK)r…r‹r>rOr†r_r¾r`)rnr¿rÀrÁrYrÂr!r!r"r«Rs&       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))rnr!r!r"rµhs   zFieldStorage.make_filei )!rqrrrsrtÚosr?ror‘r’r“rpr–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 js< *  œ             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"rc›sztest..fcSs |ƒdS)Nr!)rcr!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?ÚformrcrÇr!r!r"ÚtestŠs4            rÍcCs¤|dkr!tjƒ\}}}ddl}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)rwrmÚtbr‡rÏrvr!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•rZr!r!r"r¿s   - cCsÒt|jƒƒ}tƒtdƒ|s3tdƒtdƒx}|D]u}tdtj|ƒdddƒ||}tdtjtt|ƒƒƒd ƒtd tjt|ƒƒƒqDWtd ƒtƒd S) z$Dump the contents of a form as HTML.z

Form Contents:

z

No form fields.z

z
ú:rbú zzz
z
N)rÔr•rÉrÒrÚreprrw)rÌr•rZrmr!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'cCsetdtddƒ|jddƒ}|jddƒ}|jdd ƒ}|ra|jd d ƒ}|S) zDeprecated API.z1cgi.escape is deprecated, use html.escape insteadÚ stacklevelrFr0z&úz>r\z")rrGri)raZquoter!r!r"rs cCs:ddl}t|tƒr$d}nd}|j||ƒS)Nrs^[ -~]{0,200}[!-~]$z^[ -~]{0,200}[!-~]$)Úrer5rUÚmatch)rarßZ _vb_patternr!r!r"rM(s   rMÚ__main__)1rtÚ __version__ÚiorrrÚ collectionsrr2rÅZ urllib.parser=Z email.parserrZ email.messagerÚwarningsrrÒr�rÄÚ__all__rrr#rrr(rr8r?r r r rrdrr r rÍrrrrrrrrMrqr!r!r!r"Ús\            #   E d ÿÿ"'   /