V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
loggerhead
V2EX  ›  Python

[python] 这行注释啥意思?

  •  
  •   loggerhead ·
    loggerhead · 2015-07-22 18:39:29 +08:00 · 2712 次点击
    这是一个创建于 3416 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面的代码中那句 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
    
    7 条回复    2015-07-23 17:04:00 +08:00
    VicYu
        1
    VicYu  
       2015-07-22 19:05:09 +08:00   ❤️ 1
    loggerhead
        2
    loggerhead  
    OP
       2015-07-22 19:13:49 +08:00
    @VicYu 不是同一个东西吧
    VicYu
        3
    VicYu  
       2015-07-22 19:29:05 +08:00
    @loggerhead rfc822 http://www.ietf.org/rfc/rfc822.txt

    阿伯网英特网文字消息的标准格式
    也就是电子邮件的标准格式
    分信头和主体
    VicYu
        4
    VicYu  
       2015-07-22 19:31:10 +08:00
    @loggerhead
    这个代码实现的是http://www.ietf.org/rfc/rfc2822.txt
    2822格式的消息
    julyclyde
        5
    julyclyde  
       2015-07-22 21:31:13 +08:00   ❤️ 1
    都写了是rfc822
    http是rfc2616
    loggerhead
        6
    loggerhead  
    OP
       2015-07-23 16:51:10 +08:00
    @VicYu @julyclyde 感谢感谢~我看到werkzeug间接使用它对HTTP头部进行解析,所以先入为主的以为rfc822是HTTP相关的东西了
    julyclyde
        7
    julyclyde  
       2015-07-23 17:04:00 +08:00
    @loggerhead header那几行的格式倒是差不多的。不过From 开头这个,在邮件和http里很不同
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2650 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 15:45 · PVG 23:45 · LAX 07:45 · JFK 10:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.