Skip to content

Instantly share code, notes, and snippets.

@studiawan
Created October 24, 2016 06:32
Show Gist options
  • Save studiawan/38028b19662f9cbcfc5a4e32f45dcec5 to your computer and use it in GitHub Desktop.
Save studiawan/38028b19662f9cbcfc5a4e32f45dcec5 to your computer and use it in GitHub Desktop.
FTP client using raw socket displaying list of files and directories.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 21))
welcome_msg = s.recv(1024).strip()
# print welcome_msg
commands = ['USER 5112100155\r\n', 'PASS 123\r\n', 'TYPE I\r\n', 'EPSV\r\n', 'QUIT\r\n']
i = 1
while True:
try:
if i > len(commands):
msg = str(s.recv(1024))
print msg.strip()
s.close()
break
s.send(commands[i-1])
msg = str(s.recv(1024))
if "226" in msg:
print msg.strip()
if "Entering Extended Passive Mode" in msg:
data_port = int(msg.strip().split('|||')[1][:-3])
data_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
data_sock.connect(('localhost', data_port))
s.send('LIST\r\n')
data = data_sock.recv(4096)
msg = str(s.recv(1024)).strip()
# print msg.strip()
i += 1
except socket.error, exc:
print exc
s.close()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment