解析FTP LIST命令返回的目录列表

有个程序要分析FTP服务器返回的目录列表,本来以为比较简单,也在网上查了几个帖子,可都是一知半解的。于是下载了Filezilla的源代码,她的源文件
directorylistingparser.h
directorylistingparser.cpp
就是解析目录列表的,有同样需求的不妨看一看,还是挺费事的,不同平台要都要特殊处理。我把头文件贴出来:


InBlock.gif#ifndef __DIRECTORYLISTINGPARSER_H__
InBlock.gif#define __DIRECTORYLISTINGPARSER_H__
InBlock.gif
/* This class is responsible for parsing the directory listings returned by
InBlock.gif * the server.
InBlock.gif * Unfortunatly, RFC959 did not specify the format of directory listings, so
InBlock.gif * each server uses its own format. In addition to that, in most cases the 
InBlock.gif * listings were not designed to be machine-parsable, they were meant to be
InBlock.gif * human readable by users of that particular server.
InBlock.gif * By far the most common format is the one returned by the Unix "ls -l"
InBlock.gif * command. However, legacy systems are still in place, especially in big
InBlock.gif * companies. These often use very exotic listing styles.
InBlock.gif * Another problem are localized listings containing date strings. In some
InBlock.gif * cases these listings are ambiguous and cannot be distinguished.
InBlock.gif * Example for an ambiguous date: 04-05-06. All of the 6 permutations for
InBlock.gif * the location of year, month and day are valid dates.
InBlock.gif * Some servers send multiline listings where a single entry can span two
InBlock.gif * lines, this has to be detected as well, as far as possible.
InBlock.gif *
InBlock.gif * Some servers send MVS style listings which can consist of just the 
InBlock.gif * filename without any additional data. In order to prevent problems, this 
InBlock.gif * format is only parsed if the server is in fact recognizes as MVS server.
InBlock.gif *
InBlock.gif * Please see tests/dirparsertest.cpp for a list of supported formats and the
InBlock.gif * expected parser result.
InBlock.gif *
InBlock.gif * If adding data to the parser, it first decomposes the raw data into lines,
InBlock.gif * which then are processed further. Each line gets consecutively tested for
InBlock.gif * different formats, starting with the most common Unix style format.
InBlock.gif * Lines not containing a recognized format (e.g. a part of a multiline
InBlock.gif * entry) are rememberd and if the next line cannot be parsed either, they
InBlock.gif * get concatenated to be parsed again (and discarded if not recognized).
InBlock.gif */

InBlock.gif
class CLine;
InBlock.gif class CToken;
InBlock.gif class CControlSocket;
InBlock.gif class CDirectoryListingParser
InBlock.gif{
InBlock.gif public:
InBlock.gif  CDirectoryListingParser(CControlSocket* pControlSocket,  const CServer& server);
InBlock.gif  ~CDirectoryListingParser();
InBlock.gif
  CDirectoryListing Parse( const CServerPath &path);
InBlock.gif
   void AddData( char *pData,  int len);
InBlock.gif   void AddLine( const wxChar* pLine);
InBlock.gif
   void Reset();
InBlock.gif
   void SetTimezoneOffset( const wxTimeSpan& span) { m_timezoneOffset = span; }
InBlock.gif
   void SetServer( const CServer& server) { m_server = server; };
InBlock.gif
protected:
InBlock.gif  CLine *GetLine( bool breakAtEnd =  false);
InBlock.gif
   void ParseData( bool partial);
InBlock.gif
   bool ParseLine(CLine *pLine,  const  enum ServerType serverType,  bool concatenated);
InBlock.gif
   bool ParseAsUnix(CLine *pLine, CDirentry &entry,  bool expect_date);
InBlock.gif   bool ParseAsDos(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsEplf(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsVms(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsIbm(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseOther(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsWfFtp(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsIBM_MVS(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsIBM_MVS_PDS(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsIBM_MVS_PDS2(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsIBM_MVS_Migrated(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsMlsd(CLine *pLine, CDirentry &entry);
InBlock.gif   bool ParseAsOS9(CLine *pLine, CDirentry &entry);
InBlock.gif  
InBlock.gif   // Only call this if servertype set to ZVM since it conflicts
InBlock.gif   // with other formats.
InBlock.gif   bool ParseAsZVM(CLine *pLine, CDirentry &entry);
InBlock.gif
   // Only call this if servertype set to HPNONSTOP since it conflicts
InBlock.gif   // with other formats.
InBlock.gif   bool ParseAsHPNonstop(CLine *pLine, CDirentry &entry);
InBlock.gif
   // Date / time parsers
InBlock.gif   bool ParseUnixDateTime(CLine *pLine,  int &index, CDirentry &entry);
InBlock.gif   bool ParseShortDate(CToken &token, CDirentry &entry,  bool saneFieldOrder =  false);
InBlock.gif   bool ParseTime(CToken &token, CDirentry &entry);
InBlock.gif
   // Parse file sizes given like this: 123.4M
InBlock.gif   bool ParseComplexFileSize(CToken& token, wxLongLong& size,  int blocksize = -1);
InBlock.gif
   bool GetMonthFromName( const wxString& name,  int &month);
InBlock.gif
  CControlSocket* m_pControlSocket;
InBlock.gif
   static std::map<wxString,  int> m_MonthNamesMap;
InBlock.gif  
InBlock.gif   struct t_list
InBlock.gif  {
InBlock.gif     char *p;
InBlock.gif     int len;
InBlock.gif  };
InBlock.gif   int m_currentOffset;
InBlock.gif
  std::list<t_list> m_DataList;
InBlock.gif  std::list<CDirentry> m_entryList;
InBlock.gif
  CLine *m_prevLine;
InBlock.gif
  CServer m_server;
InBlock.gif
   bool m_fileListOnly;
InBlock.gif  std::list<wxString> m_fileList;
InBlock.gif  
InBlock.gif   bool m_maybeMultilineVms;
InBlock.gif
  wxTimeSpan m_timezoneOffset;
InBlock.gif};
InBlock.gif
#endif









本文转自 h2appy  51CTO博客,原文链接:http://blog.51cto.com/h2appy/122279,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FTP协议(文件传输协议)是一种用于在网络上传输文件的标准协议。它基于客户端服务器模型,客户端通过FTP客户端软件连接到服务器上的FTP服务器,然后进行文件传输和访问。 要解析FTP协议的代码,我们首先需要了解协议的基本原理。FTP使用两个TCP连接进行通信,一个用于控制连接,一个用于数据连接。控制连接负责发送命令和接收响应,数据连接负责传输文件数据。下面是一个简单的FTP协议解析代码的示例: ```python import socket # 创建一个TCP/IP套接字 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 建立控制连接 sock.connect(('ftp.example.com', 21)) response = sock.recv(1024) # 接收服务器的响应 print(response) # 发送用户名和密码进行登录 sock.sendall('USER username\r\n'.encode()) response = sock.recv(1024) print(response) sock.sendall('PASS password\r\n'.encode()) response = sock.recv(1024) print(response) # 发送PWD命令获取当前目录 sock.sendall('PWD\r\n'.encode()) response = sock.recv(1024) print(response) # 发送LIST命令获取目录列表 sock.sendall('LIST\r\n'.encode()) response = sock.recv(1024) print(response) # 发送QUIT命令关闭连接 sock.sendall('QUIT\r\n'.encode()) response = sock.recv(1024) print(response) # 关闭套接字 sock.close() ``` 以上示例代码使用Python的socket模块创建了一个TCP/IP套接字,并使用connect函数连接到FTP服务器的控制端口21。然后,它按照FTP协议规定的顺序发送各种命令(如用户名、密码、PWD、LIST等)并接收服务器的响应。最后,它发送QUIT命令关闭连接。 这只是一个简单的FTP协议解析的示例代码。实际上,FTP协议还有很多其他命令和功能,如上传文件、下载文件、创建目录、删除文件等。解析完整的FTP协议需要更复杂的代码逻辑和处理逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值