本文整理汇总了Python中wpull.protocol.http.request.Response.parse_status_line方法的典型用法代码示例。如果您正苦于以下问题:Python Response.parse_status_line方法的具体用法?Python Response.parse_status_line怎么用?Python Response.parse_status_line使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wpull.protocol.http.request.Response
的用法示例。
在下文中一共展示了Response.parse_status_line方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_http_header
# 需要导入模块: from wpull.protocol.http.request import Response [as 别名]
# 或者: from wpull.protocol.http.request.Response import parse_status_line [as 别名]
def get_http_header(self) -> Response:
'''Return the HTTP header.
It only attempts to read the first 4 KiB of the payload.
Returns:
Response, None: Returns an instance of
:class:`.http.request.Response` or None.
'''
with wpull.util.reset_file_offset(self.block_file):
data = self.block_file.read(4096)
match = re.match(br'(.*?\r?\n\r?\n)', data)
if not match:
return
status_line, dummy, field_str = match.group(1).partition(b'\n')
try:
version, code, reason = Response.parse_status_line(status_line)
except ValueError:
return
response = Response(status_code=code, reason=reason, version=version)
try:
response.fields.parse(field_str, strict=False)
except ValueError:
return
return response
示例2: test_parse_status_line
# 需要导入模块: from wpull.protocol.http.request import Response [as 别名]
# 或者: from wpull.protocol.http.request.Response import parse_status_line [as 别名]
def test_parse_status_line(self):
version, code, msg = Response.parse_status_line(b'HTTP/1.0 200 OK')
self.assertEqual('HTTP/1.0', version)
self.assertEqual(200, code)
self.assertEqual('OK', msg)
version, code, msg = Response.parse_status_line(
b'HTTP/1.0 404 Not Found')
self.assertEqual('HTTP/1.0', version)
self.assertEqual(404, code)
self.assertEqual('Not Found', msg)
version, code, msg = Response.parse_status_line(b'HTTP/1.1 200 OK')
self.assertEqual('HTTP/1.1', version)
self.assertEqual(200, code)
self.assertEqual('OK', msg)
version, code, msg = Response.parse_status_line(b'HTTP/1.1 200')
self.assertEqual('HTTP/1.1', version)
self.assertEqual(200, code)
self.assertEqual('', msg)
version, code, msg = Response.parse_status_line(b'HTTP/1.1 200 ')
self.assertEqual('HTTP/1.1', version)
self.assertEqual(200, code)
self.assertEqual('', msg)
version, code, msg = Response.parse_status_line(
'HTTP/1.1 200 ððð'.encode('latin-1'))
self.assertEqual('HTTP/1.1', version)
self.assertEqual(200, code)
self.assertEqual('ððð', msg)
self.assertRaises(
ProtocolError,
Response.parse_status_line, b'HTTP/1.0'
)
self.assertRaises(
ProtocolError,
Response.parse_status_line, b'HTTP/2.0'
)
version, code, msg = Response.parse_status_line(
b'HTTP/1.0 404 N\x99t \x0eounz\r\n')
self.assertEqual('HTTP/1.0', version)
self.assertEqual(404, code)
self.assertEqual(b'N\x99t \x0eounz'.decode('latin-1'), msg)