⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nntplib.py

📁 mallet是自然语言处理、机器学习领域的一个开源项目。
💻 PY
📖 第 1 页 / 共 2 页
字号:
        """Process a GROUP command.  Argument:        - group: the group name        Returns:        - resp: server response if successful        - count: number of articles (string)        - first: first article number (string)        - last: last article number (string)        - name: the group name"""        resp = self.shortcmd('GROUP ' + name)        if resp[:3] != '211':            raise NNTPReplyError(resp)        words = resp.split()        count = first = last = 0        n = len(words)        if n > 1:            count = words[1]            if n > 2:                first = words[2]                if n > 3:                    last = words[3]                    if n > 4:                        name = words[4].lower()        return resp, count, first, last, name    def help(self):        """Process a HELP command.  Returns:        - resp: server response if successful        - list: list of strings"""        return self.longcmd('HELP')    def statparse(self, resp):        """Internal: parse the response of a STAT, NEXT or LAST command."""        if resp[:2] != '22':            raise NNTPReplyError(resp)        words = resp.split()        nr = 0        id = ''        n = len(words)        if n > 1:            nr = words[1]            if n > 2:                id = words[2]        return resp, nr, id    def statcmd(self, line):        """Internal: process a STAT, NEXT or LAST command."""        resp = self.shortcmd(line)        return self.statparse(resp)    def stat(self, id):        """Process a STAT command.  Argument:        - id: article number or message id        Returns:        - resp: server response if successful        - nr:   the article number        - id:   the article id"""        return self.statcmd('STAT ' + id)    def next(self):        """Process a NEXT command.  No arguments.  Return as for STAT."""        return self.statcmd('NEXT')    def last(self):        """Process a LAST command.  No arguments.  Return as for STAT."""        return self.statcmd('LAST')    def artcmd(self, line, file=None):        """Internal: process a HEAD, BODY or ARTICLE command."""        resp, list = self.longcmd(line, file)        resp, nr, id = self.statparse(resp)        return resp, nr, id, list    def head(self, id):        """Process a HEAD command.  Argument:        - id: article number or message id        Returns:        - resp: server response if successful        - nr: article number        - id: message id        - list: the lines of the article's header"""        return self.artcmd('HEAD ' + id)    def body(self, id, file=None):        """Process a BODY command.  Argument:        - id: article number or message id        - file: Filename string or file object to store the article in        Returns:        - resp: server response if successful        - nr: article number        - id: message id        - list: the lines of the article's body or an empty list                if file was used"""        return self.artcmd('BODY ' + id, file)    def article(self, id):        """Process an ARTICLE command.  Argument:        - id: article number or message id        Returns:        - resp: server response if successful        - nr: article number        - id: message id        - list: the lines of the article"""        return self.artcmd('ARTICLE ' + id)    def slave(self):        """Process a SLAVE command.  Returns:        - resp: server response if successful"""        return self.shortcmd('SLAVE')    def xhdr(self, hdr, str):        """Process an XHDR command (optional server extension).  Arguments:        - hdr: the header type (e.g. 'subject')        - str: an article nr, a message id, or a range nr1-nr2        Returns:        - resp: server response if successful        - list: list of (nr, value) strings"""        pat = re.compile('^([0-9]+) ?(.*)\n?')        resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)        for i in range(len(lines)):            line = lines[i]            m = pat.match(line)            if m:                lines[i] = m.group(1, 2)        return resp, lines    def xover(self,start,end):        """Process an XOVER command (optional server extension) Arguments:        - start: start of range        - end: end of range        Returns:        - resp: server response if successful        - list: list of (art-nr, subject, poster, date,                         id, references, size, lines)"""        resp, lines = self.longcmd('XOVER ' + start + '-' + end)        xover_lines = []        for line in lines:            elem = line.split("\t")            try:                xover_lines.append((elem[0],                                    elem[1],                                    elem[2],                                    elem[3],                                    elem[4],                                    elem[5].split(),                                    elem[6],                                    elem[7]))            except IndexError:                raise NNTPDataError(line)        return resp,xover_lines    def xgtitle(self, group):        """Process an XGTITLE command (optional server extension) Arguments:        - group: group name wildcard (i.e. news.*)        Returns:        - resp: server response if successful        - list: list of (name,title) strings"""        line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$")        resp, raw_lines = self.longcmd('XGTITLE ' + group)        lines = []        for raw_line in raw_lines:            match = line_pat.search(raw_line.strip())            if match:                lines.append(match.group(1, 2))        return resp, lines    def xpath(self,id):        """Process an XPATH command (optional server extension) Arguments:        - id: Message id of article        Returns:        resp: server response if successful        path: directory path to article"""        resp = self.shortcmd("XPATH " + id)        if resp[:3] != '223':            raise NNTPReplyError(resp)        try:            [resp_num, path] = resp.split()        except ValueError:            raise NNTPReplyError(resp)        else:            return resp, path    def date (self):        """Process the DATE command. Arguments:        None        Returns:        resp: server response if successful        date: Date suitable for newnews/newgroups commands etc.        time: Time suitable for newnews/newgroups commands etc."""        resp = self.shortcmd("DATE")        if resp[:3] != '111':            raise NNTPReplyError(resp)        elem = resp.split()        if len(elem) != 2:            raise NNTPDataError(resp)        date = elem[1][2:8]        time = elem[1][-6:]        if len(date) != 6 or len(time) != 6:            raise NNTPDataError(resp)        return resp, date, time    def post(self, f):        """Process a POST command.  Arguments:        - f: file containing the article        Returns:        - resp: server response if successful"""        resp = self.shortcmd('POST')        # Raises error_??? if posting is not allowed        if resp[0] != '3':            raise NNTPReplyError(resp)        while 1:            line = f.readline()            if not line:                break            if line[-1] == '\n':                line = line[:-1]            if line[:1] == '.':                line = '.' + line            self.putline(line)        self.putline('.')        return self.getresp()    def ihave(self, id, f):        """Process an IHAVE command.  Arguments:        - id: message-id of the article        - f:  file containing the article        Returns:        - resp: server response if successful        Note that if the server refuses the article an exception is raised."""        resp = self.shortcmd('IHAVE ' + id)        # Raises error_??? if the server already has it        if resp[0] != '3':            raise NNTPReplyError(resp)        while 1:            line = f.readline()            if not line:                break            if line[-1] == '\n':                line = line[:-1]            if line[:1] == '.':                line = '.' + line            self.putline(line)        self.putline('.')        return self.getresp()    def quit(self):        """Process a QUIT command and close the socket.  Returns:        - resp: server response if successful"""        resp = self.shortcmd('QUIT')        self.file.close()        self.sock.close()        del self.file, self.sock        return respdef _test():    """Minimal test function."""    s = NNTP('news', readermode='reader')    resp, count, first, last, name = s.group('comp.lang.python')    print resp    print 'Group', name, 'has', count, 'articles, range', first, 'to', last    resp, subs = s.xhdr('subject', first + '-' + last)    print resp    for item in subs:        print "%7s %s" % item    resp = s.quit()    print resp# Run the test when run as a scriptif __name__ == '__main__':    _test()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -