Skip to content

Commit

Permalink
Improve unicode handling (#78)
Browse files Browse the repository at this point in the history
Allow printing of nested properties in "shodan search"
  • Loading branch information
achillean committed Sep 2, 2018
1 parent 648d125 commit bba05c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
21 changes: 8 additions & 13 deletions shodan/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,18 @@ def search(color, fields, limit, separator, query):

# Loop over all the fields and print the banner as a row
for field in fields:
tmp = ''
if field in banner and banner[field]:
field_type = type(banner[field])
tmp = u''
value = get_banner_field(banner, field)
if value:
field_type = type(value)

# If the field is an array then merge it together
if field_type == list:
tmp = u';'.join(banner[field])
tmp = u';'.join(value)
elif field_type in [int, float]:
tmp = u'{}'.format(banner[field])
tmp = u'{}'.format(value)
else:
tmp = escape_data(banner[field])
tmp = escape_data(value)

# Colorize certain fields if the user wants it
if color:
Expand Down Expand Up @@ -476,13 +477,7 @@ def stats(limit, facets, filename, query):
click.echo('Top {} Results for Facet: {}'.format(len(results['facets'][facet]), facet))

for item in results['facets'][facet]:
value = item['value']
if isinstance(value, basestring):
value = value.encode('ascii', errors='replace').decode('ascii')
else:
value = str(value)

click.echo(click.style(u'{:28s}'.format(value), fg='cyan'), nl=False)
click.echo(click.style(u'{:28s}'.format(item['value']), fg='cyan'), nl=False)
click.echo(click.style(u'{:12,d}'.format(item['count']), fg='green'))

click.echo('')
Expand Down
6 changes: 3 additions & 3 deletions shodan/cli/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def get_api_key():


def escape_data(args):
# Ensure the provided string isn't unicode data
if not isinstance(args, str):
args = args.encode('ascii', 'replace')
# Make sure the string is unicode so the terminal can properly display it
# We do it using format() so it works across Python 2 and 3
args = u'{}'.format(args)
return args.replace('\n', '\\n').replace('\r', '\\r').replace('\t', '\\t')


Expand Down

0 comments on commit bba05c9

Please sign in to comment.