本文整理匯總了Python中mimetools.Message.getmaintype方法的典型用法代碼示例。如果您正苦於以下問題:Python Message.getmaintype方法的具體用法?Python Message.getmaintype怎麽用?Python Message.getmaintype使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mimetools.Message
的用法示例。
在下文中一共展示了Message.getmaintype方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _open_stream
# 需要導入模塊: from mimetools import Message [as 別名]
# 或者: from mimetools.Message import getmaintype [as 別名]
def _open_stream(self):
self.conn.request("GET", self.path, headers=self.request_headers)
resp = self.conn.getresponse()
content_type = None
try:
if resp.status != 200 or resp.msg.getmaintype() != 'multipart':
raise ConnectionError(
u"Unexpected response: {resp.status}\n"
u"{resp.msg}\n{data}"
.format(data=resp.read(), **locals()))
log.debug("Opened stream\n%s", resp.msg)
boundary = resp.msg.getparam('boundary')
assert boundary
fp = ReadlineAdapter(resp)
while True:
sep = fp.readline().rstrip()
if not sep:
# XXX: instead of this should just read two bytes
# after the end of the data?
sep = fp.readline().rstrip()
if sep != b'--' + boundary:
if sep != b'--' + boundary + b'--':
raise StreamingError(u"Bad boundary %r" % sep)
break
msg = Message(fp, seekable=0)
content_length = int(msg['content-length'])
# XXX: impose maximum limit on content_length?
data = fp.read(content_length)
if content_type:
bad_type = msg.gettype() != content_type
else:
bad_type = msg.getmaintype() != 'image'
content_type = msg.gettype()
if bad_type:
raise StreamingError(
u"Unexpected content-type\n{msg}\n{data}"
.format(**locals()))
log.debug("Got part\n%s", msg)
yield VideoFrame(data, msg.gettype())
finally:
resp.close()