2022年python与常用文件传输协议 .pdf
《2022年python与常用文件传输协议 .pdf》由会员分享,可在线阅读,更多相关《2022年python与常用文件传输协议 .pdf(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、金枫 Intra net网安实验室作者:枫无眠版权所有 欢迎转载1/17python 与常用文件传输协议枫无眠关键词 :python,linux,Ylmfos,Ubuntu,ftp,sftp,samba.0 引言文件操作在我们测试和开发当中都有用到,本文总结了最常见的文件传输的方式。ftp 互联网中使用广泛的协议Sftp linux 系统中使用最广泛的协议Samba-liunux 和 windows 这间使用最广泛的协议。1 ftp 协议ftp 全称文件传输协议,是在互联网中使用广泛的协议。基于tcp协议,缺省端口 21.1.1 ftplib- FTP protocolclientThismo
2、dule definesthe classFTPand a few relateditems.TheFTPclassimplements the clientsideof the FTP protocol.You can usethisto writePython programs thatperforma varietyof automated FTPjobs,such as mirroringotherftpservers.Itisalsoused by themoduleurllibto handle URLs thatuse FTP. For more informationon FT
3、P(FileTransferProtocol),see InternetRFC959.Heresa sample sessionusingtheftplibmodule:fromftplibimportFTPftp= FTP(ftp.cwi.nl)# connecttohost,defaultportftp.login()# useranonymous,passwdanonymousftp.retrlines(LIST)# listdirectorycontentstotal24418drwxrwsr-x5 ftp-usrpdmaint1536Mar2009:48.dr-xr-srwt105f
4、tp-usrpdmaint1536Mar2114:32.-rw-r-r-1 ftp-usrpdmaint5305Mar2009:48INDEX.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - 金枫 Intra net网安实验室作者:枫无眠版权所有 欢迎转载2/17.ftp.retrbinary(RETRREADME,open(README,wb).write)226Transfercomplete.ftp.quit()The
5、module definesthe followingitems:classFTP(host, user,passwd, acct)Return a new instance oftheFTPclass. When hostisgiven, the method callconnect(host)ismade. When useris given, additionally themethodcalllogin(user,passwd,acct)is made (where passwd and acctdefault to the empty string when notgiven).1.
6、2 APISeveralmethods are availableintwo flavors:one forhandlingtextfilesand anotherforbinaryfiles.These are namedforthecommandwhich isused followedby lines forthe textversionorbinary forthe binaryversion.FTPinstanceshave the followingmethods:set_debuglevel(level)Setthe instance sdebugging level. This
7、 controls the amountof debuggingoutput printed.Thedefault,0, producesno debugging output. A value of1producesamoderateamountof debuggingoutput,generally a single line per request.A value of2or higher producesthe maximum amountof debugging output, logging eachline sentand received on thecontrol conne
8、ction.connect(host, port)Connectto the given host and port. The default port number is21, asspecified by theFTP protocol specification. It is rarely needed to specify a different port number. Thisfunction should be calledonly once for each instance;it shouldnot be calledat all if ahostwasgiven when
9、the instancewas created.All other methodscanonly beusedafteraconnectionhasbeenmade.getwelcome()Return the welcomemessagesent by the server in reply to the initial connection.(Thismessagesometimescontains disclaimersor help information that may be relevantto theuser.)login(user, passwd, acct)Log in a
10、sthe given user. The passwd and acctparametersareoptional and default to theempty string.Ifno useris specified,itdefaultstoanonymous.Ifuseris名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 17 页 - - - - - - - - - 金枫 Intra net网安实验室作者:枫无眠版权所有 欢迎转载3/17anonymous, the
11、 default passwd isanonymous. This function should be calledonly once for each instance,after a connectionhasbeen established;it should not becalled at all if a host and user were given when the instancewas created.Most FTPcommandsareonly allowedafter theclient hasloggedin.abort()Abort a file transfe
12、rthat is in progress.Using thisdoesnot alwayswork, but its worth atry.sendcmd(comm and)Sendasimple commandstring totheserverandreturn theresponsestring.voidcmd(comm and)Sendasimple commandstring to theserver andhandlethe response.Returnnothing if aresponsecodein therange200-299isreceived.Raiseanexce
13、ptionotherwise.retrbinary(comm and, callback, maxblocksize, r est)Retrieve a file in binary transfer mode. comm andshould be an appropriate RETRcommand:RETRfilename. The callbackfunction is called for each block of datareceived, witha single stringargument givingthe data block.The optionalmaxblocksi
14、ze argumentspecifiesthemaximum chunksize to readon the low-level socketobject createdto do the actual transfer (which will alsobe the largestsize of the datablocks passedto callback). A reasonable default ischosen.r est meansthesamething asinthetransfercmd()method.retrlines(command, callback)Retriev
15、e a file or directory listing in ASCIItransfer mode. comm andshould be anappropriateRETR command(seeretrbinary() or a LIST command(usually justthe stringLIST). The callbackfunction is calledfor each line, with the trailing CRLFstripped.The defaultcallbackprints the line tosys.stdout.set_pasv(boolean
16、)Enablepassive mode if booleanis true, other disablepassivemode. (In Python2.0 andbefore,passivemode wasoff by default; in Python2.1 andlater,it is onby default.)storbinary(comm and, file,blocksize)Storeafile in binary m andshouldbeanappropriateSTOR command:STORfilename. fileis an open file object w
17、hich is read until EOF using itsread()method in blocks of size blocksizeto provide the data to be stored. Theblocksizeargumentdefaultsto8192.Changedin version2.1:default for blocksizeadded.storlines(command, file)Storea file in ASCII transfermode. commandshouldbean appropriateSTOR command(seestorbin
18、ary(). Lines are read until EOF from theopen file object fileusing itsreadline()methodtoprovide thedatato bestored.transfercmd(cmd,rest)Initiate a transfer over the data connection.If the transfer is active,send a EPRT orPORT commandandthe transfercommandspecifiedby cmd, andaccepttheconnection.If th
19、eserver is passive,send a EPSV or PASV command,connectto it, and start thetransfercommand.Either way,return thesocketfor theconnection.Ifoptionalrest isgiven,a REST commandissentto the名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 17 页 - - - - - - - - - 金枫 Intr
20、a net网安实验室作者:枫无眠版权所有 欢迎转载4/17server,passingrest as an argument.rest isusuallya byteoffsetintothe requestedfile,tellingthe serverto restartsendingthe filesbytesat the requestedoffset,skippingoverthe initialbytes.Note however thatRFC959 requiresonlythatrest be a stringcontainingcharactersinthe printab
21、lerangefrom ASCII code 33 to ASCII code 126. Thetransfercmd()method,therefore,convertsrest to a string,but no check isperformedon the stringscontents.Ifthe serverdoes not recognizetheREST command,anerror_replyexceptionwillbe raised.Ifthishappens, simplycalltransfercmd()withouta rest argument.ntransf
22、ercmd(cmd,r est)Liketransfercmd(), but returnsa tuple of the dataconnection andtheexpectedsizeof the data. If the expectedsize could not be computed,Nonewill be returnedas theexpectedsize.cmd andrest meansthesamething asintransfercmd().nlst(argument,.)Return a list of files asreturned by the NLST co
23、mmand.The optional argumentis adirectory to list (default is the currentserverdirectory). Multiple argumentscanbe usedto passnon-standardoptions to theNLST command.dir(argument,.)Producea directory listing asreturned by the LIST command,printing it to standardoutput.Theoptional argumentis adirectory
24、 to list (default is thecurrentserver directory).Multiple argumentscanbeused to passnon-standardoptions to theLISTcommand.Ifthe lastargumentis afunction, it is usedasacallbackfunction asforretrlines(); thedefault prints tosys.stdout. Thismethod returnsNone.rename(fromname,toname )Renamefile fromname
25、on theserver totoname.delete(filename)Remove the file named filename from the server. If successful, returns the text of theresponse,otherwiseraiseserror_permon permissionerrorsorerror_replyon othererrors.cwd(pathname )Setthe currentdirectory on theserver.mkd(pathname )Createanewdirectory on theserv
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年python与常用文件传输协议 2022 python 常用 文件传输 协议
限制150内