def __init__(self): dir='E:\\Python\\apps\\simon816\\cURL\\' self.console=Console() self.console.console('curl >',"CURL") self.console.bodystyle['input']={'color':'#117711'} self.console.bodystyle['output']={'color':'#000088'} self.console.bind('exec_cmd',self.command) def p(a,r): #self.console.oldstd['out'].write(repr(a) + '\n') return r self.console.bind('get_text_css',lambda a=None: p(a,{'font-antialias':'on','font-size':'15'}) ) self.UserPassStore={} self.protocols={ 'http':[80,httplib.HTTPConnection], 'https':[443,httplib.HTTPSConnection] } if not os.path.exists(dir):os.makedirs(dir) os.chdir(dir) self.argParser=ArgumentParser() # arguments in order and agree with the specification at # http://curl.haxx.se/docs/manpage.html self.add_arg('-0',0,['--http1.0'],'Use HTTP/1.0') self.add_arg('-A',2,['--user-agent'],'Sets user-agent') self.add_arg('-b',2,['--cookie'],'Send cookie') self.add_arg('-c',2,['--cookie-jar'],'Recieve cookie') self.add_arg('--connect-timeout',2,d='Set timeout') self.add_arg('-d',2,['--data','--data-ascii'],'POST Data') self.add_arg('-D',2,['--dump-header'],'Send output to a file') self.add_arg('-e',2,['--referer'],'Set referer header') self.add_arg('-F',2,['--form'],'POST a form, usally file upload') self.add_arg('-G',0,['--get'],'Set a GET parameter') self.add_arg('-H',2,['--header'],'Set a header') self.add_arg('-i',0,['--include'],'Return headers recieved') self.add_arg('-I',0,['--head'],'Use the HEAD verb') self.add_arg('-L',0,['--location'], 'Follow Location headers when a 3XX status code is returned') self.add_arg('-o',2,['--output'],'Where to dump output') self.add_arg('-O',0,['--remote-name']) self.add_arg('-s',0,['--silent']) self.add_arg('-u',1,['--user'],'Sets the user, using Basic auth') self.add_arg('--url',2,d='Specify URL to connect to') self.add_arg('-v',0,['--verbose'],'Makes curl talk a lot') self.add_arg('-X',2,['--request'],'Set HTTP verb') self.add_arg('-h',0,['--help'],'Displays help') self.maxredirs=10 self.redirs = 0 self.console.shell_run('http://google.com')
class curl: def __init__(self): dir='E:\\Python\\apps\\simon816\\cURL\\' self.console=Console() self.console.console('curl >',"CURL") self.console.bodystyle['input']={'color':'#117711'} self.console.bodystyle['output']={'color':'#000088'} self.console.bind('exec_cmd',self.command) def p(a,r): #self.console.oldstd['out'].write(repr(a) + '\n') return r self.console.bind('get_text_css',lambda a=None: p(a,{'font-antialias':'on','font-size':'15'}) ) self.UserPassStore={} self.protocols={ 'http':[80,httplib.HTTPConnection], 'https':[443,httplib.HTTPSConnection] } if not os.path.exists(dir):os.makedirs(dir) os.chdir(dir) self.argParser=ArgumentParser() # arguments in order and agree with the specification at # http://curl.haxx.se/docs/manpage.html self.add_arg('-0',0,['--http1.0'],'Use HTTP/1.0') self.add_arg('-A',2,['--user-agent'],'Sets user-agent') self.add_arg('-b',2,['--cookie'],'Send cookie') self.add_arg('-c',2,['--cookie-jar'],'Recieve cookie') self.add_arg('--connect-timeout',2,d='Set timeout') self.add_arg('-d',2,['--data','--data-ascii'],'POST Data') self.add_arg('-D',2,['--dump-header'],'Send output to a file') self.add_arg('-e',2,['--referer'],'Set referer header') self.add_arg('-F',2,['--form'],'POST a form, usally file upload') self.add_arg('-G',0,['--get'],'Set a GET parameter') self.add_arg('-H',2,['--header'],'Set a header') self.add_arg('-i',0,['--include'],'Return headers recieved') self.add_arg('-I',0,['--head'],'Use the HEAD verb') self.add_arg('-L',0,['--location'], 'Follow Location headers when a 3XX status code is returned') self.add_arg('-o',2,['--output'],'Where to dump output') self.add_arg('-O',0,['--remote-name']) self.add_arg('-s',0,['--silent']) self.add_arg('-u',1,['--user'],'Sets the user, using Basic auth') self.add_arg('--url',2,d='Specify URL to connect to') self.add_arg('-v',0,['--verbose'],'Makes curl talk a lot') self.add_arg('-X',2,['--request'],'Set HTTP verb') self.add_arg('-h',0,['--help'],'Displays help') self.maxredirs=10 self.redirs = 0 self.console.shell_run('http://google.com') def add_arg(self,n,rd,al=None,d=''): #just a quick shortcut a=self.argParser.createArgument(name=n,requiresdata=rd,description=d) if al:a.aliases=al self.argParser.RegisterArgument(a) def command(self,cmd): # handles commands when enter is pressed try:args,argstat=self.argParser.Parse(cmd) except (self.argParser.ParseError, AttributeError), e: print e return if not args:return self.verbose=False self.silent=False self.redir=0 header={'Accept':'*/*','User-Agent':'curl/7.29.0'} verb='GET' data='' http=11 timeout=None if '-h' in args: print 'Available Options:' for arg in self.argParser.listArguments(): print ', '.join(arg)+' : '+self.argParser.getArgument(arg[0]).description return if not '--url' in args: if 'UNKNOWN' in args: args['--url']=args['UNKNOWN'] del args['UNKNOWN'] else: print 'Please specify a url' return proto,host,path,query,frag=urlsplit(args['--url'].last()) try: host,port=host.split(':') port=int(port) except:port=None #only uses the last url specified if proto in self.protocols: if not port:port=self.protocols[proto][0] else: print 'Unknown protocol %s'%repr(proto) return if not host: print 'Error: No host name' return sufx=host.split('.') if len(sufx)==1 or len(sufx[-1])<2 and (not sufx[0].isdigit()): print 'Invalid host name' return if not path: path='/' header['Host']=host if query:path+='?'+query if frag:path+='#'+frag if '-0' in args: http=10 if '-A' in args: header['User-Agent']=args['-A'].last() if '--connect-timeout' in args: try:timeout=int(args['--connect-timeout'].last()) except: print 'Invalid timeout' return if '-v' in args: self.verbose=True self.silent=False if '-s' in args: self.verbose=False self.silent=True if '-u' in args: skip=0 try:user=args['-u'].last() except IndexError: if host in self.UserPassStore: userpass=self.UserPassStore[host] skip=1 else: print args['-u'].name+' argument has no value' return def parsestr(userarg): userpass=userarg.split(':') if len(userpass)==1: pwd=self.console.prompt('Enter host password for user \'%s\': '%userpass[0],1) userpass.append(pwd) return b64e(':'.join(userpass))[:-1] if not skip: userpass=parsestr(user) if host in self.UserPassStore:action='replace' else:action='save' try: saveuser=raw_input('%s username and password for %s? [y/n]'%(action,host)).lower()=='y' except EOFError: saveuser=False if saveuser: self.UserPassStore[host]=userpass header['Authorization']='Basic '+userpass if '-b' in args: i=0 for v in args['-b'].Values(): if v[0]=='@': f=open(v[1:]) args['-b'].values[i]=f.read() f.close() i+=1 header['Cookie']=args['-b'].join(';') if '-d' in args: verb="POST" header['Content-Type']='application/x-www-form-urlencoded' data=args['-d'].last() if data=='-': data=raw_input('Send data:') elif data[0]=='@': f=open(data[1:]) data=f.read() f.close() header['Content-Length']=str(len(data)) if '-e' in args: header['Referer']=args['-e'].last() if '-H' in args: for h in args['-H'].Values(): try: s=h.split(':') k=s[0];v=':'.join(s[1:]) if not v:del header[k] else: v=v[1:] if not v:raise ValueError header[k]=v except: print 'Malformed HTTP Header: %s'%repr(h) return if '-I' in args:verb='HEAD' if '-G' in args:verb='GET' if '-X' in args:verb=args['-X'].last().upper() sockinfo=(proto,host,port,timeout) httpinfo=(verb,path,http,data,header) if str(self.console.oldstd['out']).startswith('<ped.PythonShellWindow'): # just some debugging when testing in the ped IDE print sockinfo print httpinfo print args print argstat self.download(sockinfo,httpinfo,args)