{ "metadata": { "name": "", "signature": "sha256:cafa72314c3a57e8cb6a63b4a218f47493ef8b4456254fe0e0d27b39cb90813a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Client-Side Scripting" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this chapter, we\u2019ll concentrate on the FTP and email protocol modules in Python,\n", "and we\u2019ll peek at a few others along the way (NNTP news, HTTP web pages, and so\n", "on)." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "FTP: Transferring Files over the Net" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FTP lets us ask for files from any server machine that supports FTP, without\n", "requiring that it run our custom getfile script. FTP also supports more advanced op-\n", "erations such as uploading files to the server, getting remote directory listings, and\n", "more." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Really, FTP runs on top of two sockets: one for passing control commands between\n", "client and server (port 21), and another for transferring bytes. By using a two-socket\n", "model, FTP avoids the possibility of deadlocks (i.e., transfers on the data socket do not\n", "block dialogs on the control socket). Ultimately, though, Python\u2019s ftplib support\n", "module allows us to upload and download files at a remote server machine by FTP,\n", "without dealing in raw socket calls or FTP protocol details." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Transferring Files with ftplib" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Python script to download and play a media file by FTP. Uses ftplib, the ftp\n", "protocol handler which uses sockets. Ftp runs on 2 sockets (one for data, one\n", "for control--on ports 20 and 21) and imposes message text formats, but Python's\n", "ftplib module hides most of this protocol's details. Change for your site/file." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "from getpass import getpass\n", "from ftplib import FTP" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "nonpassive = False\n", "fliename = 'monkeys.jpg'\n", "dirname = '.'\n", "sitename = 'ftp.rmi.net'\n", "userinfo = ('lutz', getpass('Pswd?'))\n", "if len(sys.argv) > 1:filename = sys.argv[1]\n", "\n", "print 'Connecting...'\n", "conncetion = FTP(sitename)\n", "conncetion.login(*useinfo)\n", "conncetion.cwd(dirname)\n", "if nonpassive: # force active FTP if server requires\n", " connection.set_pasv(False)\n", " \n", "print 'Downloading'\n", "localfile = open(filename,'wb')\n", "conncetion.retrbinary('RETR ' + filename, localfile.write, 1024)\n", "connection.quit()\n", "localfile.close()\n", "\n", "if input('Open file?') in ['Y', 'y']:\n", " from PP4E.System.Media.playfile import playfile\n", " playfile(filename)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "connection.retrbinary('RETR ' + filename, localfile.write, 1024)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we\u2019re in the target remote directory, we simply call the retrbinary method to\n", "download the target server file in binary mode. The retrbinary call will take a while to\n", "complete, since it must download a big file. It gets three arguments:\n", "- An FTP command string; here, the string RETR filename , which is the standard\n", "format for FTP retrievals.\n", "- A function or method to which Python passes each chunk of the downloaded file\u2019s\n", "bytes; here, the write method of a newly created and opened local file.\n", "- A size for those chunks of bytes; here, 1,024 bytes are downloaded at a time, but\n", "the default is reasonable if this argument is omitted." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }