[python] 这行注释啥意思?

2015-07-22 18:39:29 +08:00
 loggerhead

下面的代码中那句 Skip unix From name time lines 是啥意思?HTTP头部怎么会出现 From 开头的行?

PS:因为 seekable 输入是0,所以我删掉了一些代码,源码位于 /usr/lib/python2.7/rfc822.py

class Message:
    """Represents a single RFC 2822-compliant message."""

    def __init__(self, fp, seekable = 1):
        self.fp = fp
        self.seekable = seekable
        self.startofheaders = None
        self.startofbody = None
        self.readheaders()

    def readheaders(self):
        self.dict = {}
        self.unixfrom = ''
        self.headers = lst = []
        self.status = ''
        headerseen = ""
        firstline = 1
        startofline = unread = tell = None

        if hasattr(self.fp, 'unread'):
            unread = self.fp.unread

        while 1:
            line = self.fp.readline()
            if not line:
                self.status = 'EOF in headers'
                break
            # 下面这行啥意思?
            # Skip unix From name time lines
            if firstline and line.startswith('From '):
                self.unixfrom = self.unixfrom + line
                continue

            firstline = 0
            if headerseen and line[0] in ' \t':
                # It's a continuation line.
                lst.append(line)
                x = (self.dict[headerseen] + "\n " + line.strip())
                self.dict[headerseen] = x.strip()
                continue
            elif self.iscomment(line):
                # It's a comment.  Ignore it.
                continue
            elif self.islast(line):
                # Note! No pushback here!  The delimiter line gets eaten.
                break
            headerseen = self.isheader(line)
            if headerseen:
                # It's a legal header line, save it.
                lst.append(line)
                self.dict[headerseen] = line[len(headerseen)+1:].strip()
                continue
            elif headerseen is not None:
                # An empty header name. These aren't allowed in HTTP, but it's
                # probably a benign mistake. Don't add the header, just keep
                # going.
                continue
            else:
                # It's not a header line; throw it back and stop here.
                if not self.dict:
                    self.status = 'No headers'
                else:
                    self.status = 'Non-header line where header expected'
                # Try to undo the read.
                if unread:
                    unread(line)
                elif tell:
                    self.fp.seek(startofline)
                else:
                    self.status = self.status + '; bad seek'
                break
2711 次点击
所在节点    Python
7 条回复
VicYu
2015-07-22 19:05:09 +08:00
loggerhead
2015-07-22 19:13:49 +08:00
@VicYu 不是同一个东西吧
VicYu
2015-07-22 19:29:05 +08:00
@loggerhead rfc822 http://www.ietf.org/rfc/rfc822.txt

阿伯网英特网文字消息的标准格式
也就是电子邮件的标准格式
分信头和主体
VicYu
2015-07-22 19:31:10 +08:00
@loggerhead
这个代码实现的是http://www.ietf.org/rfc/rfc2822.txt
2822格式的消息
julyclyde
2015-07-22 21:31:13 +08:00
都写了是rfc822
http是rfc2616
loggerhead
2015-07-23 16:51:10 +08:00
@VicYu @julyclyde 感谢感谢~我看到werkzeug间接使用它对HTTP头部进行解析,所以先入为主的以为rfc822是HTTP相关的东西了
julyclyde
2015-07-23 17:04:00 +08:00
@loggerhead header那几行的格式倒是差不多的。不过From 开头这个,在邮件和http里很不同

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/207595

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX