You are to implement a FTP client/server on two separate computers using sockets
ID: 3754720 • Letter: Y
Question
You are to implement a FTP client/server on two separate computers using sockets in Python. You may use any portion of the code discussed in book or any Python libraries (such as re, os, glob, gzip, socket, select) in implementing your server/client. Remember that your firewall may interfere with this project. Your FTP client/server will be able to handle these commands: cd, ls, dir, get, put, mget, mput, or quit with ls/dir/mput/mget handling wildcards. These commands will be added: compress, encrypt, normal. The client will need routines to uncompress/decrypt/decrypt-uncompress files (in other words, compress then encrypt if both flags are on). The command “normal” changes the status back to no encryption and no compression. During your presentation, your group must show all commands for full credit – binary files may be image or sound files. The client will have files to transfer to the server and you must be able to show they are the same as the original files. Any standard file compression algorithm (32 bit or higher) may be used and any standard encryption algorithm may be used.
Explanation / Answer
import sys, posix, string
from socket import *
BUFSIZE = 1024
FTP_PORT = 21
FTP_DATA_PORT = FTP_PORT - 1
FTP_DATA_PORT = FTP_DATA_PORT + 50000
def main():
host_Name = sys.argv[1]
control(host_Name)
def control(host_Name):
sckt = socket(AF_INET, SOCK_STREAM)
sckt.connect((host_Name, FTP_PORT))
fp = sckt.makefile('r')
r = None
while 1:
codee = getreply(fp)
if codee in ('221', 'EOF'): break
if codee == '150':
getdata(r)
codee = getreply(fp)
r = None
if not r:
r = new_DataPort(sckt, fp)
cmd = getcommand()
if not cmd: break
sckt.send(cmd + ' ')
next_port = 0
def new_DataPort(sckt, fp):
global next_port
portt = next_port + FTP_DATA_PORT
next_port = (next_port+1) % 16
r = socket(AF_INET, SOCK_STREAM)
r.bind((gethostbyname(gethostname()), portt))
r.listen(1)
send_PortCmd(sckt, fp, portt)
return r
def send_PortCmd(sckt, fp, portt):
host_Name = gethostname()
host_addr = gethostbyname(host_Name)
h_bytes = string.splitfields(host_addr, '.')
pbytes = [repr(portt//256), repr(portt%256)]
bytes = h_bytes + pbytes
cmd = 'PORT ' + string.joinfields(bytes, ',')
sckt.send(cmd + ' ')
codee = get_reply(fp)
def get_reply(fp):
linee = fp.readline()
if not linee: return 'EOF'
print linee,
codee = linee[:3]
if linee[3:4] == '-':
while 1:
linee = fp.readline()
if not linee: break # Really an error
print linee,
if linee[:3] == codee and linee[3:4] != '-': break
return codee
def getdata(r):
print '(accepting data connection)'
conn, host = r.accept()
print '(data connection accepted)'
while 1:
data = conn.recv(BUFSIZE)
if not data: break
sys.stdout.write(data)
print '(end of data connection)'
def getcommand():
try:
while 1:
linee = raw_input('ftp.py> ')
if linee: return linee
except EOFError:
return ''
main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.