text: pysendfile 2.0.1
  A Python interface to sendfile(2)
  Quick links
  About). The normal way of sending a file over a socket involves reading data from the file into a userspace buffer, then write that buffer to the socket via send() or sendall():
  # how a file is tipically sent import socket file = open("somefile", "rb") sock = socket.socket() sock.connect(("127.0.0.1", 8021)) while True: chunk = file.read(65536) if not chunk: break # EOF sock.sendall(chunk)
  This copying of the data twice (once into the userland buffer, and once out from that userland buffer) imposes some performance and resource penalties. sendfile(2) syscall avoids these penalties by avoiding any use of userland buffers; it also results in a single system call (and thus only one context switch), rather than the series of read(2) / write(2) system calls (each system call requiring a context switch) used internally for the data copying.
  import socket from sendfile import sendfile file = open("somefile", "rb") blocksize = os.path.getsize("somefile") sock = socket.socket() sock.connect(("127.0.0.1", 8021)) offset = 0 while True: sent = sendfile(sock.fileno(), file.fileno(), offset, blocksize) if sent == 0: break # EOF offset += sent
  A simple benchmark
  This benchmark script implements the two examples above and compares plain socket.send() and sendfile() performances in terms of CPU time spent and bytes transmitted per second resulting in sendfile() being about 2.5x faster. These are the results I get on my Linux 2.6.38 box, AMD dual-core 1.6 GHz:
  send()
  sendfile()
  When do you want to use it?
  Basically any application sending files over the network can take advantage of sendfile(2). HTTP and FTP servers are a typical example. proftpd and vsftpd are known to use it, so is pyftpdlib.
  API documentation
  sendfile module provides a single function: sendfile().
  sendfile.sendfile(out, in, offset, nbytes, header="", trailer="", flags=0)
  Copy nbytes bytes from file descriptor in (a regular file) to file descriptor out (a socket) starting at offset. Return the number of bytes just being sent. When the end of file is reached return 0. On Linux, if offset is given as None, the bytes are read from the current position of in and the position of in is updated. headers and trailers are strings that are written before and after the data from in is written. In cross platform applications their usage is discouraged (send() or sendall() can be used instead). On Solaris, _out_ may be the file descriptor of a regular file or the file descriptor of a socket. On all other platforms, out must be the file descriptor of an open socket. flags argument is only supported on FreeBSD.
  sendfile.SF_NODISKIO
  sendfile.SF_MNOWAIT
  sendfile.SF_SYNC
  Parameters for the _flags_ argument, if the implementation supports it. They are available on FreeBSD platforms. See FreeBSD’s man sendfile(2).
  Differences with send()
  - sendfile(2) works with regular (mmap-like) files only (e.g. you can’t use it with a StringIO object).
  - Also, it must be clear that the file can only be sent “as is” (e.g. you can’t modify the content while transmitting). retry, ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED in case of disconnection. Some examples: benchmark script, test suite, pyftpdlib wrapper.
  Supported platforms
  This module works with Python versions from 2.5 to 3.4. The supported platforms are:
  - Linux
  - Mac OSX
  - FreeBSD
  - Dragon Fly BSD
  - Sun OS
  - AIX (not properly tested)
  Support
  Feel free to mail me at g.rodola [AT] gmail [DOT] com or post on the the mailing list:.
  Status
  As of now the code includes a solid test suite and its ready for production use. It’s been included in pyftpdlib project and used in production environments for almost a year now without any problem being reported so far.
  - Author: Giampaolo Rodola
  - Keywords: sendfile,python,performance,ftp
  - License: MIT
  - Platform: UNIX
  - Categories
  - Development Status :: 5 - Production/Stable
  - Intended Audience :: Developers
  - License :: OSI Approved :: MIT License
  - Operating System :: MacOS :: MacOS X
  - Operating System :: POSIX :: AIX
  - Operating System :: POSIX :: BSD
  - Operating System :: POSIX :: BSD :: FreeBSD
  - Operating System :: POSIX :: Linux
  - Operating System :: POSIX :: SunOS/Solaris
  - Programming Language :: C
  - Programming Language :: Python :: 2.5
  - Programming Language :: Python :: 2.6
  - Programming Language :: Python :: 2.7
  - Programming Language :: Python :: 3
  - Programming Language :: Python :: 3.0
  - Programming Language :: Python :: 3.1
  - Programming Language :: Python :: 3.2
  - Programming Language :: Python :: 3.3
  - Programming Language :: Python :: 3.4
  - Topic :: Internet :: File Transfer Protocol (FTP)
  - Topic :: Internet :: WWW/HTTP
  - Topic :: System :: Networking
  - Topic :: System :: Operating System
  - Package Index Owner: giampaolo.rodola
  - DOAP record: pysendfile-2.0.1.xml