0

I am using python socket to connect to ftp.rediris.es and I don't receive the answer I expect after sending data. I explain this better with my code and answers: This is my code (test.py)

#!/usr/bin/env python

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print "Socket Created"

port = 21

host = "ftp.rediris.es"

ip = socket.gethostbyname(host)

print ip

print "ip of " +host+ " is " +ip

s.connect ((ip, port))

print "Socket Connected to "+host+" on ip "+ ip

message = "HELP\r\n"

s.sendall(message)

reply = s.recv(65565)

print reply

This is the answer when I run the code:

 python test.py
Socket Created
130.206.1.5
ip of ftp.rediris.es is 130.206.1.5
Socket Connected to ftp.rediris.es on ip 130.206.1.5
220-  Bienvenido al FTP anónimo de RedIRIS.
220-Welcome to the RedIRIS anonymous FTP server.
220 Only anonymous FTP is allowed here

And this is what I expect:

telnet
telnet> open ftp.rediris.es 21
Trying 130.206.1.5...
Connected to zeppo.rediris.es.
Escape character is '^]'.
220-  Bienvenido al FTP anónimo de RedIRIS.
220-Welcome to the RedIRIS anonymous FTP server.
220 Only anonymous FTP is allowed here
HELP
214-The following SITE commands are recognized
 ALIAS
 CHMOD
 IDLE
 UTIME

I have tried this on the port 80 towards www.google.com, sending a GET / HTTP/1.1\r\n\r\n and seen the header perfectly. What happens¿? Am I not sending the command right to the server¿? Thank you in advance

aDoN
  • 1,877
  • 4
  • 39
  • 55

1 Answers1

1

You may check if the last line of 220 Only anonymous FTP is allowed here has been received before sending the HELP message, something like read_until in telnetlib.

Like this, it works for me:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Socket Created"
port = 21
host = "ftp.rediris.es"
ip = socket.gethostbyname(host)

print ip
print "ip of " +host+ " is " +ip

s.connect ((ip, port))
print "Socket Connected to "+host+" on ip "+ ip

reply = ''
while True:
    message = "HELP\r\n"
    reply += s.recv(1024)
    if not reply:
        break
    if '220 Only anonymous FTP is allowed here' in reply:
        s.sendall(message)
        break    
reply += s.recv(65535)
print reply

Printout:

Socket Created
130.206.1.5
ip of ftp.rediris.es is 130.206.1.5
Socket Connected to ftp.rediris.es on ip 130.206.1.5
220-  Bienvenido al FTP anónimo de RedIRIS.
220-Welcome to the RedIRIS anonymous FTP server.
220 Only anonymous FTP is allowed here
214-The following SITE commands are recognized
 ALIAS
 CHMOD
 IDLE
 UTIME
214 Pure-FTPd - http://pureftpd.org/

That said though, not entirely sure why you haven't chosen the more suitable modules like ftplib or telnetlib to begin with.

Anzel
  • 19,825
  • 5
  • 51
  • 52
  • Thank you very much really, that was helpful. So to make sure I have understood, my code didn't work because I was sending "HELP" too soon right¿? Before receiving all I needed to receive. And you put the code in the while why¿? Because you are all the time receving don't you ¿? I am not using other libs because I am used to sockets I am new here and want to start with the basics. – aDoN Nov 03 '14 at 19:46
  • @user3515313, yes and no. your `s.recv(65535)` should really be reading by chunks instead (I've updated to 1024) as well. In socket, the message you received is not guaranteed, depending on network load etc. so it's better to try wrapping in a `while` loop so no data will be missed until you reach the point you can send a message to the server. That said, your original code will work too (usually), and you need to take care of some corner case (connection failure etc.). There could be many exceptions to *except*, but it's a general idea how we deal with sending commands to telnet/ftp alike – Anzel Nov 04 '14 at 12:04