#!/usr/bin/python3
import socket
import re

host='jupiter.challenges.picoctf.org'
port=29221
flb=''
flo=''
flh=''

tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp.connect((host, port))
# Binary
r = tcp.recv(1024)
resp = r.decode('utf-8')
bins = re.findall(r'[0-1]\w+', resp)

for binary in bins:
    flb += chr(int(binary,2))

print('Binary:\n', bins, '->', flb)

flb += '\r\n'
tcp.sendall(flb.encode('utf-8'))

#Octal
r = tcp.recv(1024)
resp = r.decode('utf-8')
octs = re.findall(r'[0-9]\w+', resp)

for octal in octs:
    flo+=hex(int(str(octal), 8)).replace('0x','')

flo = bytes.fromhex(flo).decode('utf-8')
print('Octal:\n', octs, '->', flo)
flo += '\r\n'
tcp.sendall(flo.encode('utf-8'))

# Hexadecimal
r = tcp.recv(1024)
resp = r.decode('utf-8')

hexa = resp.split(' ')
flh = bytes.fromhex(hexa[4]).decode('utf-8')
hexa = [hexa[4]]
print('Hexadecimal:\n', hexa, '->', flh)

flh += '\r\n'
tcp.sendall(flh.encode('utf-8'))

r = tcp.recv(1024)
resp = r.decode('utf-8')
print('\n',resp)

