twisted.web.static.File(resource.Resource, filepath.FilePath)
class documentationtwisted.web.static
View Source
(View In Hierarchy)
File is a resource that represents a plain non-interpreted file (although it can look for an extension like .rpy or .cgi and hand the file to a processor for interpretation if you wish). Its constructor takes a file path.
Alternatively, you can give a directory path to the constructor. In this case the resource will represent that directory, and its children will be files underneath that directory. This provides access to an entire filesystem tree with a single Resource.
If you map the URL 'http://server/FILE' to a resource created as File('/tmp'), then http://server/FILE/ will return an HTML-formatted listing of the /tmp/ directory, and http://server/FILE/foo/bar.html will return the contents of /tmp/foo/bar.html .
Class Variable | childNotFound | Resource
used to render 404 Not Found error pages. |
Class Variable | forbidden | Resource
used to render 403 Forbidden error pages. |
Method | __init__ | Create a file with the given path. |
Method | ignoreExt | Ignore the given extension. |
Method | directoryListing | Undocumented |
Method | getChild | If this File 's
path refers to a directory, return a File referring to the
file named path in that directory. |
Method | openForReading | Open a file and return it. |
Method | getFileSize | Return file size. |
Method | makeProducer | Make a StaticProducer
that will produce the body of this response. |
Method | render_GET | Begin sending the contents of this File (or a subset of
the contents, based on the 'range' header) to the given request. |
Method | redirect | Undocumented |
Method | listNames | Undocumented |
Method | listEntities | Undocumented |
Method | createSimilarFile | Undocumented |
Method | _parseRangeHeader | Parse the value of a Range header into (start, stop) pairs. |
Method | _rangeToOffsetAndSize | Convert a start and end from a Range header to an offset and size. |
Method | _contentRange | Return a string suitable for the value of a Content-Range header for a range with the given offset and size. |
Method | _doSingleRangeRequest | Set up the response for Range headers that specify a single range. |
Method | _doMultipleRangeRequest | Set up the response for Range headers that specify a single range. |
Method | _setContentHeaders | Set the Content-length and Content-type headers for this request. |
Inherited from AbstractFilePath (via FilePath):
Method | getContent | Retrieve the file-like object for this file path. |
Method | parents | Retrieve an iterator of all the ancestors of this path. |
Method | children | List the children of this path object. |
Method | walk | Yield myself, then each of my children, and each of those children's children in turn. |
Method | sibling | Return a FilePath with
the same directory as this instance but with a basename of
path . |
Method | descendant | Retrieve a child or child's child of this path. |
Method | segmentsFrom | Return a list of segments between a child and its ancestor. |
Method | __hash__ | Hash the same as another FilePath with
the same path as mine. |
Method | getmtime | Deprecated. Use getModificationTime instead. |
Method | getatime | Deprecated. Use getAccessTime instead. |
Method | getctime | Deprecated. Use getStatusChangeTime instead. |
Inherited from AbstractFilePath (via FilePath):
Method | getContent | Retrieve the file-like object for this file path. |
Method | parents | Retrieve an iterator of all the ancestors of this path. |
Method | children | List the children of this path object. |
Method | walk | Yield myself, then each of my children, and each of those children's children in turn. |
Method | sibling | Return a FilePath with
the same directory as this instance but with a basename of
path . |
Method | descendant | Retrieve a child or child's child of this path. |
Method | segmentsFrom | Return a list of segments between a child and its ancestor. |
Method | __hash__ | Hash the same as another FilePath with
the same path as mine. |
Method | getmtime | Deprecated. Use getModificationTime instead. |
Method | getatime | Deprecated. Use getAccessTime instead. |
Method | getctime | Deprecated. Use getStatusChangeTime instead. |
Inherited from AbstractFilePath (via FilePath):
Method | getContent | Retrieve the file-like object for this file path. |
Method | parents | Retrieve an iterator of all the ancestors of this path. |
Method | children | List the children of this path object. |
Method | walk | Yield myself, then each of my children, and each of those children's children in turn. |
Method | sibling | Return a FilePath with
the same directory as this instance but with a basename of
path . |
Method | descendant | Retrieve a child or child's child of this path. |
Method | segmentsFrom | Return a list of segments between a child and its ancestor. |
Method | __hash__ | Hash the same as another FilePath with
the same path as mine. |
Method | getmtime | Deprecated. Use getModificationTime instead. |
Method | getatime | Deprecated. Use getAccessTime instead. |
Method | getctime | Deprecated. Use getStatusChangeTime instead. |
Create a file with the given path.
Parameters | path | The filename of the file from which this File will serve data. (type: str ) |
defaultType | A major/minor-style MIME type specifier indicating the
Content-Type with which this File 's data will be
served if a MIME type cannot be determined based on path 's
extension. (type: str ) | |
ignoredExts | A sequence giving the extensions of paths in the filesystem which will be
ignored for the purposes of child lookup. For example, if
ignoredExts is (".bar",) and
path is a directory containing a file named
"foo.bar" , a request for the
"foo" child of this resource will succeed with a File pointing to
"foo.bar" . | |
registry | The registry object being used to handle this request. If
None , one will be created. (type: Registry ) | |
allowExt | Ignored parameter, only present for backwards compatibility. Do not pass a value for this parameter. |
If this File
's
path refers to a directory, return a File
referring to the
file named path
in that directory.
If path
is the empty string, return a DirectoryLister
instead.
Parse the value of a Range header into (start, stop) pairs.
In a given pair, either of start or stop can be None, signifying that no value was provided, but not both.
Returns | A list [(start, stop)] of pairs of length at least one. | |
Raises | ValueError | if the header is syntactically invalid or if the Bytes-Unit is anything other than 'bytes'. |
Convert a start and end from a Range header to an offset and size.
This method checks that the resulting range overlaps with the resource
being served (and so has the value of getFileSize()
as an
indirect input).
Either but not both of start or end can be None
:
End is interpreted as inclusive, as per RFC 2616.
If this range doesn't overlap with any of this resource, (0,
0)
is returned, which is not otherwise a value return value.
Parameters | start | The start value from the header, or None if one was not
present. |
end | The end value from the header, or None if one was not present. | |
Returns | (offset, size) where offset is how far into this resource this
resource the range begins and size is how long the range is, or (0,
0) if the range does not overlap this resource. |
Return a string suitable for the value of a Content-Range header for a range with the given offset and size.
The offset and size are not sanity checked in any way.
Parameters | offset | How far into this resource the range begins. |
size | How long the range is. | |
Returns | The value as appropriate for the value of a Content-Range header. |
Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response code and Content-Range header appropriately. The return value indicates which part of the resource to return.
Parameters | request | The Request object. |
startAndEnd | A 2-tuple of start of the byte range as specified by the header and the end
of the byte range as specified by the header. At most one of the start and
end may be None . | |
Returns | A 2-tuple of the offset and size of the range to return. offset == size == 0 indicates that the request is not satisfiable. |
Set up the response for Range headers that specify a single range.
This method checks if the request is satisfiable and sets the response code and Content-Type and Content-Length headers appropriately. The return value, which is a little complicated, indicates which parts of the resource to return and the boundaries that should separate the parts.
In detail, the return value is a tuple rangeInfo rangeInfo
is a list of 3-tuples (partSeparator, partOffset, partSize)
.
The response to this request should be, for each element of
rangeInfo
, partSeparator
followed by
partSize
bytes of the resource starting at
partOffset
. Each partSeparator
includes the
MIME-style boundary and the part-specific Content-type and Content-range
headers. It is convenient to return the separator as a concrete string
from this method, because this method needs to compute the number of bytes
that will make up the response to be able to set the Content-Length header
of the response accurately.
Parameters | request | The Request object. |
byteRanges | A list of (start, end) values as specified by the header. For
each range, at most one of start and end may be
None . | |
Returns | See above. |
Set the Content-length and Content-type headers for this request.
This method is not appropriate for requests for multiple byte ranges; _doMultipleRangeRequest
will set these headers in that case.
Parameters | request | The Request object. |
size | The size of the response. If not specified, default to
self.getFileSize() . |
Make a StaticProducer
that will produce the body of this response.
This method will also set the response code and Content-* headers.
Parameters | request | The Request object. |
fileForReading | The file object containing the resource. | |
Returns | A StaticProducer .
Calling .start() on this will begin producing the response. |