def urlrequest(*args): """ .. function:: urlrequest([null], url) -> response This functions connects to the *url* (via GET HTTP method) and returns the request's result. If first parameter is *null*, then in case of errors *null* will be returned. Examples: >>> sql("select urlrequest('http://www.google.com/not_existing')") Traceback (most recent call last): ... HTTPError: HTTP Error 404: Not Found >>> sql("select urlrequest(null, 'http://www.google.com/not_existing') as result") result ------ None """ try: req = urllib2.Request(''.join((x.encode('utf-8') for x in args if x != None)), None, domainExtraHeaders) hreq = urllib2.urlopen(req) if [1 for x,y in hreq.headers.items() if x.lower() in ('content-encoding', 'content-type') and y.lower().find('gzip')!=-1]: hreq = gzip.GzipFile(fileobj=hreq) return unicode(hreq.read(), 'utf-8', errors = 'replace') except (urllib2.HTTPError, urllib2.URLError),e: if args[0] == None: return None else: raise e
def urlrequestpost(*args): """ .. function:: urlrequestpost(data_jdict, [null], url) -> response This functions connects to the *url* (via POST HTTP method), submits the *data_jdict*, and returns the request's result. If second parameter is *null*, then in case of errors *null* will be returned. Examples: >>> sql('''select urlrequestpost('{"POST_param_name":"data"}', 'http://www.google.com/not_existing')''') Traceback (most recent call last): ... HTTPError: HTTP Error 404: Not Found >>> sql('''select urlrequestpost('["POST_param_name","data"]', null, 'http://www.google.com/not_existing') as result''') result ------ None >>> sql("select urlrequestpost(jdict('param1','value1'), null, 'http://www.google.com/not_existing') as result") result ------ None >>> sql("select urlrequestpost(jpack('param1','value1'), null, 'http://www.google.com/not_existing') as result") result ------ None """ try: req = urllib2.Request(''.join((x for x in args[1:] if x != None)), None, domainExtraHeaders) datain = jopts.fromjsingle(args[0]) dataout = [] if type(datain) == list: for i in xrange(0, len(datain), 2): dataout.append((datain[i].encode('utf_8'), datain[i+1].encode('utf_8'))) else: dataout = [( x.encode('utf_8'), y.encode('utf_8') ) for x,y in datain.items()] if dataout == []: raise functions.OperatorError('urlrequestpost',"A list or dict should be provided") hreq = urllib2.urlopen(req, urllib.urlencode(dataout)) if [1 for x,y in hreq.headers.items() if x.lower() in ('content-encoding', 'content-type') and y.lower().find('gzip')!=-1]: hreq = gzip.GzipFile(fileobj=hreq) return unicode(hreq.read(), 'utf-8', errors = 'replace') except urllib2.HTTPError,e: if args[1] == None: return None else: raise e