test_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 » test_sendfile.py
# -*- Mode: Python; tab-width: 4 -*-

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

import asyncore
import os
import socket
import string

# server: just run the script with no args
# client: python test_sendfile.py -c <remote-host> <remote-filename>

if __name__ == '__main__':
    import sys
    if '-c' in sys.argv:
        import operator
        import socket
        s = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
        host = sys.argv[2]
        s.connect ((host, 9009))
        fname = sys.argv[3]
        s.send (fname + '\r\n')
        size = string.atoi (s.recv(8), 16)
        total = 0
        blocks = []
        while (total < size):
            block = s.recv (8192)
            if not block:
                break
            else:
                total = total + len(block)
                blocks.append (block)
        import sys
        for b in blocks:
            sys.stdout.write (b)
    else:
        import asynchat_sendfile
        
        class test_channel (asynchat_sendfile.async_chat_with_sendfile):
        
            def __init__ (self, conn, addr):
                asynchat_sendfile.async_chat_with_sendfile.__init__ (self, conn)
                self.set_terminator ('\r\n')
                self.buffer = ''
                
            def collect_incoming_data (self, data):
                self.buffer = self.buffer + data
                
            def found_terminator (self):
                filename, self.buffer = self.buffer, ''
                if filename:
                    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, self.sendfile_callback)
                    self.close_when_done()
                else:
                    self.push ('ok, bye\r\n')
                    self.close_when_done()
                    
            def sendfile_callback (self, success, fd):
                os.close (fd)
                
        class test_server (asyncore.dispatcher):
            def __init__ (self, addr=('', 9009)):
                self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
                self.set_reuse_addr()
                self.bind (addr)
                self.listen (2048)
                print 'server started on', addr
                
            def handle_accept (self):
                conn, addr = self.accept()
                test_channel (conn, addr)
                
        test_server()
        asyncore.loop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.