通過代理服務器訪問FTP的Python代碼

b573 9年前發布 | 2K 次閱讀 Python

import urllib2

Install proxy support for urllib2

proxy_info = { 'host' : 'proxy.myisp.com', 'port' : 3128, } proxy_support = urllib2.ProxyHandler({"ftp" : "http://%(host)s:%(port)d" % proxy_info}) opener = urllib2.build_opener(proxy_support) urllib2.install_opener(opener)

List the content of a directory (it returns an HTML page built by the proxy)

(You will have to parse the HTML to extract the list of files and directories.)

print urllib2.urlopen("ftp://login:password@server/directory").read()

Download a file:

data = urllib2.urlopen("ftp://login:password@server/directory/myfile.zip").read() open("myfile.zip","w+b").write(data)</pre>

 本文由用戶 b573 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!