+++ /dev/null
-# A simple FTP client.\r
-#\r
-# The information to write this program was gathered from RFC 959,\r
-# but this is not a complete implementation! Yet it shows how a simple\r
-# FTP client can be built, and you are welcome to extend it to suit\r
-# it to your needs...\r
-#\r
-# How it works (assuming you've read the RFC):\r
-#\r
-# User commands are passed uninterpreted to the server. However, the\r
-# user never needs to send a PORT command. Rather, the client opens a\r
-# port right away and sends the appropriate PORT command to the server.\r
-# When a response code 150 is received, this port is used to receive\r
-# the data (which is written to stdout in this version), and when the\r
-# data is exhausted, a new port is opened and a corresponding PORT\r
-# command sent. In order to avoid errors when reusing ports quickly\r
-# (and because there is no s.getsockname() method in Python yet) we\r
-# cycle through a number of ports in the 50000 range.\r
-\r
-\r
-import sys, posix, string\r
-from socket import *\r
-\r
-\r
-BUFSIZE = 1024\r
-\r
-# Default port numbers used by the FTP protocol.\r
-#\r
-FTP_PORT = 21\r
-FTP_DATA_PORT = FTP_PORT - 1\r
-\r
-# Change the data port to something not needing root permissions.\r
-#\r
-FTP_DATA_PORT = FTP_DATA_PORT + 50000\r
-\r
-\r
-# Main program (called at the end of this file).\r
-#\r
-def main():\r
- hostname = sys.argv[1]\r
- control(hostname)\r
-\r
-\r
-# Control process (user interface and user protocol interpreter).\r
-#\r
-def control(hostname):\r
- #\r
- # Create control connection\r
- #\r
- s = socket(AF_INET, SOCK_STREAM)\r
- s.connect((hostname, FTP_PORT))\r
- f = s.makefile('r') # Reading the replies is easier from a file...\r
- #\r
- # Control loop\r
- #\r
- r = None\r
- while 1:\r
- code = getreply(f)\r
- if code in ('221', 'EOF'): break\r
- if code == '150':\r
- getdata(r)\r
- code = getreply(f)\r
- r = None\r
- if not r:\r
- r = newdataport(s, f)\r
- cmd = getcommand()\r
- if not cmd: break\r
- s.send(cmd + '\r\n')\r
-\r
-\r
-# Create a new data port and send a PORT command to the server for it.\r
-# (Cycle through a number of ports to avoid problems with reusing\r
-# a port within a short time.)\r
-#\r
-nextport = 0\r
-#\r
-def newdataport(s, f):\r
- global nextport\r
- port = nextport + FTP_DATA_PORT\r
- nextport = (nextport+1) % 16\r
- r = socket(AF_INET, SOCK_STREAM)\r
- r.bind((gethostbyname(gethostname()), port))\r
- r.listen(1)\r
- sendportcmd(s, f, port)\r
- return r\r
-\r
-\r
-# Send an appropriate port command.\r
-#\r
-def sendportcmd(s, f, port):\r
- hostname = gethostname()\r
- hostaddr = gethostbyname(hostname)\r
- hbytes = string.splitfields(hostaddr, '.')\r
- pbytes = [repr(port//256), repr(port%256)]\r
- bytes = hbytes + pbytes\r
- cmd = 'PORT ' + string.joinfields(bytes, ',')\r
- s.send(cmd + '\r\n')\r
- code = getreply(f)\r
-\r
-\r
-# Process an ftp reply and return the 3-digit reply code (as a string).\r
-# The reply should be a line of text starting with a 3-digit number.\r
-# If the 4th char is '-', it is a multi-line reply and is\r
-# terminate by a line starting with the same 3-digit number.\r
-# Any text while receiving the reply is echoed to the file.\r
-#\r
-def getreply(f):\r
- line = f.readline()\r
- if not line: return 'EOF'\r
- print line,\r
- code = line[:3]\r
- if line[3:4] == '-':\r
- while 1:\r
- line = f.readline()\r
- if not line: break # Really an error\r
- print line,\r
- if line[:3] == code and line[3:4] != '-': break\r
- return code\r
-\r
-\r
-# Get the data from the data connection.\r
-#\r
-def getdata(r):\r
- print '(accepting data connection)'\r
- conn, host = r.accept()\r
- print '(data connection accepted)'\r
- while 1:\r
- data = conn.recv(BUFSIZE)\r
- if not data: break\r
- sys.stdout.write(data)\r
- print '(end of data connection)'\r
-\r
-# Get a command from the user.\r
-#\r
-def getcommand():\r
- try:\r
- while 1:\r
- line = raw_input('ftp.py> ')\r
- if line: return line\r
- except EOFError:\r
- return ''\r
-\r
-\r
-# Call the main program.\r
-#\r
-main()\r