import os
import sys
import urllib
'''This script is the client for invoking RESTful Web services.'''

outputFile = open(sys.argv[1],'w')
resUrl = sys.argv[2]

if(resUrl.find('__tilda__')>-1):
    ulist = resUrl.split('__tilda__')
    resUrl = '~'.join(ulist)
    
paramdict={}
length=(len(sys.argv))

i=3
while( i>=3 and i<(length-1)):
    key=sys.argv[i]
    val=sys.argv[i+1]
    if val=='fileInput':
        print '##########fileInput found#############'
        f=open(sys.argv[i+2])
        line = f.readline().strip('\n')
        line = line.strip(' ')
        val = line        
        i=i+1
    
    while(val.find('**')>-1):
        vlist = val.split('**')
	val = ' '.join(vlist)
    print key + '\t' + val
    paramdict[key]=val
    i=i+2

params = urllib.urlencode(paramdict)
data = urllib.urlopen(resUrl, params).read()
outputFile.write(data)
outputFile.close()
