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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。