"""Response classes used by urllib.The base class, addbase, defines a minimal file-like interface,including read() and readline(). The typical response object is anaddinfourl instance, which defines an info() method that returnsheaders and a geturl() method that returns the url."""import tempfile__all__ = ['addbase', 'addclosehook', 'addinfo', 'addinfourl']class addbase(tempfile._TemporaryFileWrapper):"""Base class for addinfo and addclosehook. Is a good idea for garbage collection."""# XXX Add a method to expose the timeout on the underlying socket?def __init__(self, fp):super(addbase, self).__init__(fp, '<urllib response>', delete=False)# Keep reference around as this was part of the original API.self.fp = fpdef __repr__(self):return '<%s at %r whose fp = %r>' % (self.__class__.__name__,id(self), self.file)def __enter__(self):if self.fp.closed:raise ValueError("I/O operation on closed file")return selfdef __exit__(self, type, value, traceback):self.close()class addclosehook(addbase):"""Class to add a close hook to an open file."""def __init__(self, fp, closehook, *hookargs):super(addclosehook, self).__init__(fp)self.closehook = closehookself.hookargs = hookargsdef close(self):try:closehook = self.closehookhookargs = self.hookargsif closehook:self.closehook = Noneself.hookargs = Noneclosehook(*hookargs)finally:super(addclosehook, self).close()class addinfo(addbase):"""class to add an info() method to an open file."""def __init__(self, fp, headers):super(addinfo, self).__init__(fp)self.headers = headersdef info(self):return self.headersclass addinfourl(addinfo):"""class to add info() and geturl() methods to an open file."""def __init__(self, fp, headers, url, code=None):super(addinfourl, self).__init__(fp, headers)self.url = urlself.code = codedef getcode(self):return self.codedef geturl(self):return self.url
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。