分析服务器发送来的PORT信息,和服务器的port建立数据连接。
2 3 #!/usr/bin/python 4 # -*- coding: utf-8 -*- 5 import ftplib 6 import os 7 import socket 8 import sys 9 10 HOST = '12.15.26.25' 11 DIRN = '/file_yes' 12 FILE = 'put.txt' 13 host1 = '11.25.45.26' 14 port1 = 15245 15 16 17 18 def main(): 19 try: 20 f = ftplib.FTP(HOST) 21 except (socket.error, socket.gaierror): 22 print 'ERROR:cannot reach " %s"' % HOST 23 return 24 print '***Connected to host "%s"' % HOST 25 26 try: 27 f.login('FTP002','tianyan360!@#') 28 except ftplib.error_perm: 29 print 'ERROR: cannot login FTP' 30 f.quit() 31 return 32 print '*** Logged in as "FTP"' 33 print f.getwelcome() 34 35 try: 36 f.sendcmd('SYST') 37 f.sendcmd('PWD') 38 f.sendcmd('CWD /file_yes') 39 f.sendcmd('PWD') 40 f.sendcmd('TYPE I') 41 str1= f.sendcmd('PASV') 42 43 print str1 44 str2=str1.split(',') 45 print str2 46 print str2[4] 47 str3=str2[5].split(')') 48 print str3 49 print str3[0] 50 port = int(str2[4])*256+int(str3[0]) 51 print port 52 53 except ftplib.error_perm: 54 print 'ERROR:error with putcmd' 55 f.quit() 56 return 57 print '***putcmd successfully' 58 59 #To establish a TCP connection 60 try: 61 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 62 except socket.error,msg: 63 print 'Failed to create socket.Error code:'+str(msg[0])+',Error message'+msg[1] 64 sys.exit() 65 print 'Socket Create' 66 67 s.connect((host1,port)) 68 print 'Socket connect!' 69 print f.sendcmd('LIST')
#get file from ftp and print in screen
#print f.sendcmd('RETR wordless10m.docx')
#print s.recv(1024)
#print 'translate successfully!'
70 71 f.quit() 72 return 73 if __name__ == '__main__': 74 main()