Guido van Rossum | b83ec8f | 1992-05-19 13:52:02 +0000 | [diff] [blame^] | 1 | # A simple FTP client. |
| 2 | # |
| 3 | # The information to write this program was gathered from RFC 959, |
| 4 | # but this is not a complete implementation! Yet it shows how a simple |
| 5 | # FTP client can be built, and you are welcome to extend it to suit |
| 6 | # it to your needs... |
| 7 | |
| 8 | |
| 9 | import sys, posix, string |
| 10 | from socket import * |
| 11 | |
| 12 | |
| 13 | BUFSIZE = 1024 |
| 14 | |
| 15 | # Default port numbers used by the FTP protocol. |
| 16 | # |
| 17 | FTP_PORT = 21 |
| 18 | FTP_DATA_PORT = FTP_PORT - 1 |
| 19 | |
| 20 | # Change the data port to something not needing root permissions. |
| 21 | # |
| 22 | FTP_DATA_PORT = FTP_DATA_PORT + 50000 |
| 23 | |
| 24 | |
| 25 | # Main program (called at the end of this file). |
| 26 | # |
| 27 | def main(): |
| 28 | hostname = sys.argv[1] |
| 29 | control(hostname) |
| 30 | |
| 31 | |
| 32 | # Control process (user interface and user protocol interpreter). |
| 33 | # |
| 34 | def control(hostname): |
| 35 | # |
| 36 | # Create control connection |
| 37 | # |
| 38 | s = socket(AF_INET, SOCK_STREAM) |
| 39 | s.connect(hostname, FTP_PORT) |
| 40 | f = s.makefile('r') # Reading the replies is easier from a file... |
| 41 | # |
| 42 | # Control loop |
| 43 | # |
| 44 | r = None |
| 45 | while 1: |
| 46 | code = getreply(f) |
| 47 | if code in ('221', 'EOF'): break |
| 48 | if code == '150': |
| 49 | getdata(r) |
| 50 | code = getreply(f) |
| 51 | r = None |
| 52 | if not r: |
| 53 | r = newdataport(s, f) |
| 54 | cmd = getcommand() |
| 55 | if not cmd: break |
| 56 | s.send(cmd + '\r\n') |
| 57 | |
| 58 | |
| 59 | # Create a new data port and send a PORT command to the server for it. |
| 60 | # (Cycle through a number of ports to avoid problems with reusing |
| 61 | # a port within a short time.) |
| 62 | # |
| 63 | cycle = [0] |
| 64 | # |
| 65 | def newdataport(s, f): |
| 66 | port = cycle[0] |
| 67 | cycle[0] = (port+1) % 16 |
| 68 | port = port + FTP_DATA_PORT |
| 69 | r = socket(AF_INET, SOCK_STREAM) |
| 70 | r.bind(gethostbyname(gethostname()), port) |
| 71 | r.listen(0) |
| 72 | sendportcmd(s, f, port) |
| 73 | return r |
| 74 | |
| 75 | |
| 76 | # Send an appropriate port command. |
| 77 | # |
| 78 | def sendportcmd(s, f, port): |
| 79 | hostname = gethostname() |
| 80 | hostaddr = gethostbyname(hostname) |
| 81 | hbytes = string.splitfields(hostaddr, '.') |
| 82 | pbytes = [`port/256`, `port%256`] |
| 83 | bytes = hbytes + pbytes |
| 84 | cmd = 'PORT ' + string.joinfields(bytes, ',') |
| 85 | s.send(cmd + '\r\n') |
| 86 | code = getreply(f) |
| 87 | |
| 88 | |
| 89 | # Process an ftp reply and return the 3-digit reply code (as a string). |
| 90 | # The reply should be a line of text starting with a 3-digit number. |
| 91 | # If the 4th char is '-', it is a multi-line reply and is |
| 92 | # terminate by a line starting with the same 3-digit number. |
| 93 | # Any text while receiving the reply is echoed to the file. |
| 94 | # |
| 95 | def getreply(f): |
| 96 | line = f.readline() |
| 97 | if not line: return 'EOF' |
| 98 | print line, |
| 99 | code = line[:3] |
| 100 | if line[3:4] == '-': |
| 101 | while 1: |
| 102 | line = f.readline() |
| 103 | if not line: break # Really an error |
| 104 | print line, |
| 105 | if line[:3] == code: break |
| 106 | return code |
| 107 | |
| 108 | |
| 109 | # Get the data from the data connection. |
| 110 | # |
| 111 | def getdata(r): |
| 112 | print '(accepting data connection)' |
| 113 | conn, host = r.accept() |
| 114 | print '(data connection accepted)' |
| 115 | while 1: |
| 116 | data = conn.recv(BUFSIZE) |
| 117 | if not data: break |
| 118 | sys.stdout.write(data) |
| 119 | print '(end of data connection)' |
| 120 | |
| 121 | # Get a command from the user. |
| 122 | # |
| 123 | def getcommand(): |
| 124 | try: |
| 125 | return raw_input('ftp> ') |
| 126 | except EOFError: |
| 127 | return '' |
| 128 | |
| 129 | |
| 130 | # Call the main program. |
| 131 | # |
| 132 | main() |