asynchat_sendfile.py :  » Web-Frameworks » Zope » Zope-2.6.0 » ZServer » medusa » sendfile » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Zope 
Zope » Zope 2.6.0 » ZServer » medusa » sendfile » asynchat_sendfile.py
# -*- Mode: Python; tab-width: 4 -*-

RCS_ID = '$Id: asynchat_sendfile.py,v 1.3 2001/05/01 11:45:27 andreas Exp $'

import sendfile
import asynchat

async_chat = asynchat.async_chat

# we can't call sendfile() until ac_out_buffer is empty.

class async_chat_with_sendfile (async_chat):

        # if we are in the middle of sending a file, this will be overriden
    _sendfile = None
    
    def push_sendfile (self, fd, offset, bytes, callback=None):
            # we set out_buffer_size to zero in order to keep async_chat
            # from calling refill_buffer until the whole file has been sent.
        self._saved_obs = self.ac_out_buffer_size
        self.ac_out_buffer_size = 0
        self._sendfile = (fd, offset, bytes, callback)
        
    def initiate_send (self):
        if self._sendfile is None:
            async_chat.initiate_send (self)
        else:
            if len(self.ac_out_buffer):
                async_chat.initiate_send (self)
            else:
                fd, offset, bytes, callback = self._sendfile
                me = self.socket.fileno()
                try:
                    sent = sendfile.sendfile (fd, me, offset, bytes)
                    offset = offset + sent
                    bytes = bytes - sent
                    if bytes:
                        self._sendfile = (fd, offset, bytes, callback)
                    else:
                        self._sendfile = None
                        self.ac_out_buffer_size = self._saved_obs
                        if callback is not None:
                                # success
                            callback (1, fd)
                except:
                    self._sendfile = None
                    self.ac_out_buffer_size = self._saved_obs
                    # failure
                    if callback is not None:
                        callback (0, fd)
                        
                        # here's how you might use this:
                        # fd = os.open (filename, os.O_RDONLY, 0644)
                        # size = os.lseek (fd, 0, 2)
                        # os.lseek (fd, 0, 0)
                        # self.push ('%08x' % size)
                        # self.push_sendfile (fd, 0, size)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.