Can someone show me an example of how to serve static files with Twisted (the fastest way)? (I will also be serving dynamic content.) Thanx.
On Fri, Apr 25, 2008 at 4:41 PM, inhahe
Can someone show me an example of how to serve static files with Twisted (the fastest way)? (I will also be serving dynamic content.) Thanx.
http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.h...
If I want my static .html/whatever files to be gzip'd when the client
supports it, does that mean I have to serve them myself and can't use
static.File?
On Fri, Apr 25, 2008 at 10:52 AM, Graeme Glass
On Fri, Apr 25, 2008 at 4:41 PM, inhahe
wrote: Can someone show me an example of how to serve static files with Twisted (the fastest way)? (I will also be serving dynamic content.) Thanx.
http://twistedmatrix.com/projects/web/documentation/howto/using-twistedweb.h...
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
inhahe
If I want my static .html/whatever files to be gzip'd when the client supports it, does that mean I have to serve them myself and can't use static.File?
Not necessarily, since you can handle the gzip operation above the
level of the static.File resource, though you may need to get slightly
more involved in handling the request than you would otherwise.
Here's what I created for one of my servers that uses twisted.web in
order to support the gzip encoding (note some debugging prints are
still present). First, I have a wrapper for a Request object that
will handle gzipping any data generated for the client that the
Request object generates, including with a producer/consumer model
such as that used by static.File.
- - - - - - - - - - - - - - - - - - - - - - - - -
import struct
import zlib
class GzipRequest(object):
"""Wrapper for a request that applies a gzip content encoding"""
def __init__(self, request, compressLevel=6):
self.request = request
self.request.setHeader('Content-Encoding', 'gzip')
# Borrowed from twisted.web2 gzip filter
self.compress = zlib.compressobj(compressLevel, zlib.DEFLATED,
-zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL,0)
def __getattr__(self, attr):
if 'request' in self.__dict__:
return getattr(self.request, attr)
else:
raise AttributeError, attr
def __setattr__(self, attr, value):
if 'request' in self.__dict__:
return setattr(self.request, attr, value)
else:
self.__dict__[attr] = value
def write(self, data):
if not self.request.startedWriting:
print 'GzipRequest: Initializing'
self.crc = zlib.crc32('')
self.size = self.csize = 0
# XXX: Zap any length for now since we don't know final size
if 'content-length' in self.request.headers:
del self.request.headers['content-length']
# Borrow header information from twisted.web2 gzip filter
self.request.write('\037\213\010\000' '\0\0\0\0' '\002\377')
self.crc = zlib.crc32(data, self.crc)
self.size += len(data)
cdata = self.compress.compress(data)
self.csize += len(cdata)
print 'GzipRequest: ' \
'Writing %d bytes, %d total (%d compressed, %d total)' % \
(len(data),self.size,len(cdata),self.csize)
if cdata:
self.request.write(cdata)
elif self.request.producer:
# Simulate another pull even though it hasn't really made it
# out to the consumer yet.
self.request.producer.resumeProducing()
def finish(self):
remain = self.compress.flush()
self.csize += len(remain)
print 'GzipRequest: Finishing (size %d, compressed %d)' % (self.size,
self.csize)
if remain:
self.request.write(remain)
self.request.write(struct.pack('
Hi all Running on twisted 2.5 OpenBSD I see the following traceback in the logs every twenty minutes with up to 200 athena/LivePages connected: Traceback (most recent call last): --- <exception caught here> --- File "/usr/local/lib/python2.5/site-packages/twisted/internet/selectreactor.py", line 148, in _doReadOrWrite why = getattr(selectable, method)() File "/usr/local/lib/python2.5/site-packages/twisted/internet/tcp.py", line 362, in doRead return self.protocol.dataReceived(data) File "/usr/local/lib/python2.5/site-packages/twisted/protocols/basic.py", line 233, in dataReceived why = self.lineReceived(line) File "/usr/local/lib/python2.5/site-packages/twisted/web/http.py", line 1069, in lineReceived self.allContentReceived() File "/usr/local/lib/python2.5/site-packages/twisted/web/http.py", line 1110, in allContentReceived req.requestReceived(command, path, version) File "/usr/local/lib/python2.5/site-packages/twisted/web/http.py", line 603, in requestReceived self.host = self.channel.transport.getHost() File "/usr/local/lib/python2.5/site-packages/twisted/internet/tcp.py", line 672, in getHost return address.IPv4Address('TCP', *(self.socket.getsockname() + ('INET',))) File "<string>", line 1, in getsockname socket.error: (22, 'Invalid argument') Changing reactors does not help, the error is very unspecific. Before I start changing the source sprinkling it with log statements is there anybody out there who has a cure or knows an explanation. Thank you so much, Werner Thie
participants (4)
-
David Bolen
-
Graeme Glass
-
inhahe
-
Werner Thie