- 内容主要是请求到ftp服务器,然后下载一个文件。没有实现模块化,只是简单的实现了。(这都哭了)
- 遇到的问题:对于 ftp需要的两个请求没有清楚和对ftp服务器的机制没有弄清楚
- 在代码中说明
#download a folder's files
import socket
class ParseUrl():
"""docstring for ParseUrl"""
def __init__(self, url):
self.url = url
self.method = ""
self.host = ""
self.port =""
def get_method(self):
method_url = self.url
self.method = method_url.split(":")[0]
print "Request Method:",self.method
return self.method
def get_host(self):
temp_pos = self.url.find("://")
temp_url = self.url[temp_pos+3:]
self.host = temp_url[:temp_url.find(":")]
print "Request Host:",self.host
return self.host
def get_port(self):
tem_url = self.url.split(":")[2]
if tem_url.find("/"):
tem_url = tem_url[:tem_url.find("/")]
print "Request Port:",tem_url
self.port = tem_url
return self.port
def parse(self):
temp_methd = self.get_method()
temp_host = self.get_host()
temp_port = self.get_port()
return temp_methd,temp_host,temp_port
def main():
#ftp url 这一步是解析一个ftp请求 得到host和port
request_url = "ftp://localhost:2121/"
parse_url = ParseUrl(request_url)
method,host,port = parse_url.parse()
print "method,host,port",method,host,port
#get connection to ftp server
ftp_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ftp_socket.connect((host,int(port)))
user = "admin
"
password = "admin
"
print ftp_socket.recv(1024)
ftp_socket.send("USER "+user)
print ftp_socket.recv(1024)
ftp_socket.send("PASS "+password)
print ftp_socket.recv(1024)
ftp_socket.send("CWD
")
print ftp_socket.recv(1024)
ftp_socket.send("TYPE ASCII
")
print ftp_socket.recv(1024)
ftp_socket.send("SIZE my.txt
")
temp_size = ftp_socket.recv(1024)
file_size = int(temp_size.split(" ")[1])
print file_size
#LIST
ftp_socket.send("PASV
")
new_port = ftp_socket.recv(1024)
print new_port
before_port = ""
temp_port =""
if new_port.startswith("227"):
temp_port = new_port.split(",")[5]
before_port = new_port.split(",")[4]
temp_port = temp_port.replace(")
","")
#get the server's new port
new_request_port = int(before_port)*256+int(temp_port)
# connect to the server's new port
ftp_data_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ftp_data_socket.connect((host,new_request_port))
ftp_socket.send("LIST
")
print ftp_data_socket.recv(1024).decode("utf-8")
ftp_socket.recv(1024)
#RETR
ftp_socket.send("PASV
")
new_port = ftp_socket.recv(1024)
print new_port
before_port = ""
temp_port =""
if new_port.startswith("227"):
temp_port = new_port.split(",")[5]
before_port = new_port.split(",")[4]
temp_port = temp_port.replace(")
","")
#get the server's new port
new_request_port = int(before_port)*256+int(temp_port)
# connect to the server's new port
ftp_data_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ftp_data_socket.connect((host,new_request_port))
ftp_socket.send("RETR my.txt
")
file_content = ftp_data_socket.recv(file_size)
print file_content
download_file = open("my.txt","wb")
download_file.write(file_content)
download_file.close()
#print create_data_connection(ftp_socket,"LIST","",host)
if __name__ == '__main__':
main()