Trying to create a whois in python but it doesn't run

87 Views Asked by At

For some reason, the output returns like this instead of how it should be: Listed; one under the other; organized.

Reference: https://i.stack.imgur.com/HkKDZ.png

consulta.py

from socket import socket, AF_INET, SOCK_STREAM
from sys import argv
import pyfiglet

ascii_banner = pyfiglet.figlet_format("WHOIS - Gustang")
print (ascii_banner)

host = 'whois.iana.org'
port = 43
conn = host, port
crlf = b'\r\n'
bufsiz = 2172

if len(argv) == 2:

        with socket(AF_INET, SOCK_STREAM) as s:
                s.connect(conn)
                ba = bytearray()
                ba.endswith(crlf)
                s.send(f'{argv[1]}\r\n'.encode())
                resp = s.recv(bufsiz)
                print (resp)

1

There are 1 best solutions below

0
Dumbo On

Your output looks to be raw bytes (notice the b in front). The recv returns the raw bytes: https://docs.python.org/3/library/socket.html#socket.socket.recv My guess would be that casting it to a string print(resp.decode("utf-8")) will render the "\r\n" correctly. See Convert bytes to a string