[Zope-Checkins] CVS: Zope2 - asynchat_sendfile.py:1.3 test_sendfile.py:1.3
andreas@serenade.digicool.com
andreas@serenade.digicool.com
Tue, 1 May 2001 07:45:57 -0400
Update of /cvs-repository/Zope2/ZServer/medusa/sendfile
In directory serenade.digicool.com:/tmp/cvs-serv12359/sendfile
Modified Files:
asynchat_sendfile.py test_sendfile.py
Log Message:
we *hate* tabs - lets get rid of them
--- Updated File asynchat_sendfile.py in package Zope2 --
--- asynchat_sendfile.py 2001/04/25 19:09:54 1.2
+++ asynchat_sendfile.py 2001/05/01 11:45:27 1.3
@@ -11,47 +11,47 @@
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)
+ # 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)
--- Updated File test_sendfile.py in package Zope2 --
--- test_sendfile.py 2001/04/25 19:09:54 1.2
+++ test_sendfile.py 2001/05/01 11:45:27 1.3
@@ -9,70 +9,70 @@
# 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()
+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()