#!/usr/bin/env python

import socket
import sys
import os

def socket_send(ip):
    try:
        listen =socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    except socket.error, msg:
        print 'Failed to create socket. Error code: '+ str(msg[0]) + ', Error message : ' + msg[1]
        sys.exit()
    print 'socket created!'
    try:
        listen.bind(('0.0.0.0',1234))
    except socket.error, msg:
        print 'Bind Failed, Error code : ' + str(msg[0]) + 'Message ' + msg[1]
        sys.exit(1)
    print 'socket bind complete!'
    listen.listen(10)
    print 'socket now listening!'
    sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.connect((ip,1000))
    while True:
    	try:
    		conn,addr=sock.accept()
            print 'Got connection from', addr
            script_name = conn.recv(2048)
            r = os.popen('/ves_ihep_scripts/'+script_name).read()
            sock.send(r)
            sock.close()
    	except KeyboardInterrupt:
    		print("Now we will exit")
    		sys.exit(0)

if __name__=='__main__':
	socket_send("10.214.144.188")


