py3 fetch command: it may actually be better to try to print bytes as-is

This commit is contained in:
Konstantin Lopuhin 2016-01-19 16:42:34 +03:00
parent 47d3c63338
commit c6d013ec85
1 changed files with 8 additions and 4 deletions

View File

@ -1,11 +1,11 @@
from __future__ import print_function
import sys, six
from w3lib.url import is_url
from scrapy.commands import ScrapyCommand
from scrapy.http import Request
from scrapy.exceptions import UsageError
from scrapy.utils.spider import spidercls_for_request, DefaultSpider
from scrapy.utils.python import to_unicode
class Command(ScrapyCommand):
@ -29,10 +29,10 @@ class Command(ScrapyCommand):
help="print response HTTP headers instead of body")
def _print_headers(self, headers, prefix):
prefix = prefix.encode()
for key, values in headers.items():
for value in values:
print('%s %s: %s' % (
prefix, to_unicode(key), to_unicode(value)))
self._print_bytes(prefix + b' ' + key + b': ' + value)
def _print_response(self, response, opts):
if opts.headers:
@ -40,7 +40,11 @@ class Command(ScrapyCommand):
print('>')
self._print_headers(response.headers, '<')
else:
print(to_unicode(response.body))
self._print_bytes(response.body)
def _print_bytes(self, bytes_):
bytes_writer = sys.stdout if six.PY2 else sys.stdout.buffer
bytes_writer.write(bytes_ + b'\n')
def run(self, args, opts):
if len(args) != 1 or not is_url(args[0]):