📄 116.html
字号:
<Pre>
<b><I>r</I></B><B>.s_execfile(</b><b><i>code</i></B><B>)</B> </Pre>
<p>Like <tT CLAss="monofont">r_execfile()</tt> except that access to <TT CLass="monofont">sys.stdin</tt>, <tt class="monofont">sys.stdout</tt>, and <tt clasS="monofont">sys.stderr</tt> is allowed.</P>
<p>The following methods are called implicitly by code executing in the restricted environment and can be redefined in subclasses of <tt Class="monofont">RExec</Tt>:</p>
<pRE>
<B><I>r</i></b><b>.r_import(</b><B><I>modulename</I></B> <b>[,</b> <b><i>globals</I></B> <B>[,</B> <b><i>locals</i></b> <B>[,</B> <B><I>fromlist</i></b><b>]]])</b> </pre>
<p>Imports a module <i><tt class="monofont">modulename</tt></i>
. An <tT clAss="monofont">ImportError</tT> exception should be raised if the module is unsafe.</p>
<pre>
<B><i>r</i></b><B>.r_open(</B><B><I>filename</i></b> <b>[,</b> <B><I>mode</I></B> <b>[,</b> <b><i>bufsize</I></B><B>]])</B> </pre>
<p>Opens a file in the restricted environment. The arguments are the same as the built-in <TT CLass="monofont">open()</tt> function. By default, files can be opened for reading, but not for writing.</p>
<pre>
<b><i>r</i></b><b>.r_reload(</b><b><i>module</i></b><b>)</B> </prE>
<p>Reloads the module object <i><tT clasS="monofont">module</tt></i>
.</P>
<PRE>
<b><i>r</i></b><B>.r_unload(</B><B><I>module</i></b><b>)</b> </PRE>
<P>Unloads the module object <i><tt cLASS="monofont">module</tt></i>
.</p>
<pre>
<b><i>r</i></b><b>.s_import(</b><b><i>modulename</i></b> <b>[,</b> <b><I>globals</i></b> <B>[,</b> <b><i>locals</I></b> <b>[,</b> <b><I>fromlist</i></b><b>]]])</B> </PRE>
<p>Like <tt cLASS="monofont">r_import()</tt>, but with access to standard I/O streams.</p>
<pRE>
<B><I>r</i></b><b>.s_reload(</b><B><I>module</I></B><b>)</b> </pre>
<p>Like <tt class="monofont">r_reload()</tt>, but with access to standard I/O streams.</p>
<pre>
<b><I>r</i></b><B>.s_unload(</b><b><i>module</I></b><b>)</b> </pRe>
<p>Like <tT CLAss="monofont">r_unload()</tt>, but with access to standard I/O streams.</P>
<H5>Example</H5>
<P>The following program executes Python code submitted through a CGI script in a restricted environment along with limits on CPU and memory usage:</p>
<pre>
#!/usr/local/bin/python
import rexec
import cgi, StringIO, sys, string, resource
form = cgi.FieldStorage()
code = form["code"].value # Get some arbitrary code to execute
code = string.replace(code,"\015","")
sys.stderr = sys.stdout # Make error messages appear
print "Content-type: text/plain\n\n"
print "The output of your program is : \n\n"
class CGIExec(rexec.RExec):
def r_open(*args):
raise SystemError, "open not supported"
r = CGIExec() # Create sandbox
# Restrict memory usage to 4 Mbytes
resource.setrlimit(resource.RLIMIT_DATA,(4000000,4000000))
# Set CPU time limit to 10 seconds
resource.setrlimit(resource.RLIMIT_CPU,(10,10))
# Go run the code
r.s_exec(code) # Execute the untrusted code </PRE>
<P>? <b>See Also</b> <a hREF="116#6.html">Bastion</A> (283).</p>
<a name="6"></a>
<h4><tt class="monofont">Bastion</tt></h4>
<p>The <tT clAss="monofont">Bastion</tT> module restricts access to attributes of objects. It抯 primarily used in conjunction with the <tt clAss="monofont">rexec</tT> module when a privileged program wants to allow restricted programs to access attributes of unrestricted objects. The idea behind a <TT Class="monofont">Bastion</TT> is simple梐 wrapper is placed around an object, causing every method access to be redirected through a filter function that抯 responsible for accepting or rejecting the access. Furthermore, all access to data attributes (non-methods) is prohibited.</P>
<Pre>
<b>Bastion(</b><B><I>object</I></B> <b>[,</b> <b><i>filter</I></B> <B>[,</B> <b><i>name</i></b> <b>[,</b> <b><i>class</i></b><b>]]])</b> </pre>
<p>Returns a bastion for the object <i><tt cLasS="monofont">object</tt></i>
. <I><tt clAss="monofont">filter</tT></I>
is a function that accepts a string containing a method name and returns true or false if access to the method is permitted or denied, respectively. <I><Tt claSS="monofont">name</TT></i>
is the name of the object that抯 printed by the bastion抯 <tt cLASS="monofont">str()</tt> method. <i><tT CLAss="monofont">class</tt></i>
is the class object that implements Bastion objects and is not described here (it抯 rarely necessary to supply this).</p>
<h5>Example</h5>
<p>In this example, you want to restrict access to a <tt class="monofont">StringIO</tt> object so that only read operations are permitted (see the <a hRef="110#18.html">StringIO</A> module):</p>
<prE>
import StringIO, Bastion
str = StringIO("")
...
strbast = Bastion.Bastion(str, lambda x: x in ['read','readline','readlines'])
strbast.readline() # Okay
strbast.write("Ha ha") # Fails. AttributeError : write </pre>
<h5>Notes</H5>
<ul>
<lI>
<P>If the <I><Tt claSS="monofont">filter</TT></i>
function is omitted, a bastion limits access to all methods beginning with an underscore.</p>
</li>
<LI>
<P>Bastions cannot be placed around built-in types such as files and sockets.</P>
</li>
</ul></FONT>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="115.html" title="Internet Data Handling and Encoding"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=116" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="116.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="117.html" title="Miscellaneous Modules"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -