Загрузка данных


import socket
import json
import re

HOST = 'tasks.duckerz.ru'
PORT = 30009

def test_answer(answer_val):
    """Пробует отправить ответ в JSON."""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(5.0)
    try:
        s.connect((HOST, PORT))
        data = b""
        while b"Send to Bob:" not in data:
            chunk = s.recv(4096)
            if not chunk:
                break
            data += chunk
        
        text = data.decode('utf-8', errors='ignore')
        match = re.search(r'\{.*\}', text)
        if not match:
            return None
        
        alice = json.loads(match.group())
        p = int(alice['p'], 16)
        g = int(alice['g'], 16)
        A = int(alice['A'], 16)
        
        # Формируем ответ в зависимости от переданного значения
        if answer_val == 'A': ans = A
        elif answer_val == 'A^g': ans = A ^ g
        elif answer_val == 'A-g': ans = A - g
        elif answer_val == 'A//g': ans = A // g
        elif answer_val == 'A%g': ans = A % g
        elif answer_val == 'A+g': ans = A + g
        elif answer_val == 'A*g': ans = A * g
        elif answer_val == 'p': ans = p
        else: ans = int(answer_val)
        
        payload = json.dumps({"answer": ans})
        s.sendall((payload + "\n").encode())
        
        resp = s.recv(4096).decode('utf-8', errors='ignore').strip()
        if resp and "Connection terminated" not in resp and "Expecting" not in resp:
            print(f"[+] УСПЕХ с '{answer_val}'!")
            print(f"    Ответ: {resp}")
            return resp
    except Exception as e:
        pass
    finally:
        s.close()
    return None

# Перебираем операции
operations = ['A', 'A^g', 'A-g', 'A//g', 'A%g', 'A+g', 'A*g', 'p']

print("[*] Перебираем операции над A и g...")
for op in operations:
    print(f"[*] Пробуем: {op}")
    resp = test_answer(op)
    if resp:
        print(f"\n[!] НАЙДЕНО: {op}")
        print(f"[!] Ответ: {resp}")
        break
else:
    print("\n[-] Ничего не сработало. Пробуем другие подходы...")