Example #1
0
    def send_request(self, verb, url, data, data_size, headers={}):
        logger.info('ll httplibcurl -X "%s" "%s" ', verb, url)
        for (k, v) in headers.items():
            logger.debug('> %s: %s' % (k, v))
        logger.debug('\n')
#logger.debug('> ' + shorten(data, 1024))
        o = urlparse(url)
        host = o.netloc
        path = o.path
        if o.query: 
            path+='?'
            path+=o.query

        if o.scheme == 'https':
            conn = httplib.HTTPSConnection(host)
        else:
            conn = httplib.HTTPConnection(host)

        conn.putrequest ( verb, path )
        if 'content-length' not in headers:
            headers['content-length'] = data_size

        for key in headers.keys ( ):
            conn.putheader ( str ( key ), str ( headers[key] ) )

        conn.endheaders ( )
        return conn
Example #2
0
    def send_request(self, verb, url, data, data_size, headers={}):
        logger.info('ll httplibcurl -X "%s" "%s" ', verb, url)
        for (k, v) in headers.items():
            logger.debug('> %s: %s' % (k, v))
        logger.debug('\n')
        #logger.debug('> ' + shorten(data, 1024))
        o = urlparse(url)
        host = o.netloc
        path = o.path
        if o.query:
            path += '?'
            path += o.query

        if o.scheme == 'https':
            conn = httplib.HTTPSConnection(host)
        else:
            conn = httplib.HTTPConnection(host)

        conn.putrequest(verb, path)
        if 'content-length' not in headers:
            headers['content-length'] = data_size

        for key in headers.keys():
            conn.putheader(str(key), str(headers[key]))

        conn.endheaders()
        return conn
def select_best_httpc():
    import platform
    try:
        import pycurl
        logger.debug('use pycurl httpclient')
        return PyCurlHTTPC
    except :
        logger.debug('use httplib httpclient')
        return HttplibHTTPC
Example #4
0
def select_best_httpc():
    import platform
    try:
        import pycurl
        logger.debug('use pycurl httpclient')
        return PyCurlHTTPC
    except:
        logger.debug('use httplib httpclient')
        return HttplibHTTPC
def updateUserInfluence(user, delta):
	user.influence_factor = user.influence_factor + delta
	user.save()
	if 'SERVER_SOFTWARE' in os.environ:
		from bae.api.rank import BaeRank
		from bae.api import logging
		r = BaeRank("UserInfluence")
		user_key = str(user.id)
		user_influence = int(user.influence_factor)
		user_dict = {user_key: user_influence}
		logging.debug(str(user_dict))
		r.set(**user_dict)

	return
Example #6
0
def sendCheckToUser(user, resurl):
	#check user
	check_code = random.getrandbits(60)	
	print "check_code is " + str(check_code)
	user_account = user.account
	user.check_code = check_code
	user.check_status = True
	user.save()

	check_URL = resurl + str(user.id) + '/' + str(check_code)
	if 'SERVER_SOFTWARE' in os.environ:
		logging.debug("send mail")
		check_URL = 'http://sjtucourse.duapp.com/' + check_URL
		content = CHECK_EMAIL_CONTENT % check_URL
		
		from bae.api.bcms import BaeBcms	
		# bcms = BaeBcms(ACCESS_KEY, SECRET_KEY)
		bcms = BaeBcms(ACCESS_KEY, SECRET_KEY)
		logging.debug("init object")
		logging.debug("create Queue")
		real_qname = "4665ae9f84f7c7700dfbc46dc9e73d61"
		ret = bcms.mail(real_qname, content, [user_account], "*****@*****.**", CHECK_EMAIL_SUBJECT)
		logging.debug("have send mail")
		return True
	else:
		check_URL = '127.0.0.1:8000/' +  check_URL
		content = CHECK_EMAIL_CONTENT % check_URL
		try:
			send_mail(CHECK_EMAIL_SUBJECT, content, '*****@*****.**', [user_account], fail_silently=True)
		except smtplib.SMTPException:
			return False
		return True
Example #7
0
 def request(self, verb, url, data, headers={}):
     response = self.send_request(verb, url, data, headers)
     if verb == 'HEAD':
         response.close()
         resp_body = ''
     else:
         resp_body = response.read()
     for (k, v) in response.getheaders():
         logger.debug('< %s: %s' % (k, v))
     logger.debug('< ' + shorten(data, 1024))
     rst = { 'status': response.status, 
             'header' : dict(response.getheaders()), 
             'body': resp_body, 
             'body_file': None, 
             }
     if (response.status in [200, 206]): 
         return rst
     else:
         raise HTTPException(rst)
Example #8
0
    def send_request(self, verb, url, data, headers={}):
        logger.info('ll httplibcurl -X "%s" "%s" ', verb, url)
        for (k, v) in headers.items():
            logger.debug('> %s: %s' % (k, v))
        logger.debug('\n')
#logger.debug('> ' + shorten(data, 1024))
        o = urlparse(url)
        host = o.netloc
        path = o.path
        if o.query: 
            path+='?'
            path+=o.query

        if o.scheme == 'https':
            conn = httplib.HTTPSConnection(host)
        else:
            conn = httplib.HTTPConnection(host)
        conn.request(verb, path, data, headers)
        response = conn.getresponse()
        return response
Example #9
0
 def request(self, verb, url, data, headers={}):
     response = self.send_request(verb, url, data, headers)
     if verb == 'HEAD':
         response.close()
         resp_body = ''
     else:
         resp_body = response.read()
     for (k, v) in response.getheaders():
         logger.debug('< %s: %s' % (k, v))
     logger.debug('< ' + shorten(data, 1024))
     rst = {
         'status': response.status,
         'header': dict(response.getheaders()),
         'body': resp_body,
         'body_file': None,
     }
     if (response.status in [200, 206]):
         return rst
     else:
         raise HTTPException(rst)
Example #10
0
    def request(self, verb, url, data, headers={}, upload_callback=None):
        if isinstance(data, basestring):
            fp = StringIO(data)
        elif self._is_buffer(data):
            fp = data
        fp.seek(os.SEEK_SET, os.SEEK_END)
        size = fp.tell()
        fp.seek(0)

        conn = self.send_request(verb, url, data, size, headers)

        buff = fp.read(self.BUFFER_SIZE)
        loaded = len(buff)
        while len(buff):
            conn.send(buff)
            if callable(upload_callback):
                upload_callback(loaded, size)

            buff = fp.read(self.BUFFER_SIZE)

        response = conn.getresponse()
        if verb == 'HEAD':
            response.close()
            resp_body = ''
        else:
            resp_body = response
        for (k, v) in response.getheaders():
            logger.debug('< %s: %s' % (k, v))


#logger.debug('< ' + shorten(data, 1024))
        rst = {
            'status': response.status,
            'header': dict(response.getheaders()),
            'body': resp_body,
            'body_file': None,
        }
        if (response.status in [200, 206]):
            return rst
        else:
            raise HTTPException(rst)
Example #11
0
    def request(self, verb, url, data, headers={}, upload_callback = None ):
        if isinstance ( data, basestring ):
            fp = StringIO ( data )
        elif self._is_buffer ( data ):
            fp = data
        fp.seek ( os.SEEK_SET, os.SEEK_END )
        size = fp.tell ( )
        fp.seek ( 0 )

        conn = self.send_request(verb, url, data, size, headers)

        buff = fp.read ( self.BUFFER_SIZE )
        loaded = len ( buff )
        while len ( buff ):
            conn.send ( buff )
            if callable ( upload_callback ):
                upload_callback ( loaded, size )

            buff = fp.read ( self.BUFFER_SIZE ) 

        response = conn.getresponse ( )
        if verb == 'HEAD':
            response.close()
            resp_body = ''
        else:
            resp_body = response
        for (k, v) in response.getheaders():
            logger.debug('< %s: %s' % (k, v))
#logger.debug('< ' + shorten(data, 1024))
        rst = { 'status': response.status, 
                'header' : dict(response.getheaders()), 
                'body': resp_body, 
                'body_file': None, 
                }
        if (response.status in [200, 206]): 
            return rst
        else:
            raise HTTPException(rst)
    def _curl_log(self, debug_type, debug_msg):
        curl_out = [    pycurl.INFOTYPE_HEADER_OUT,         #2  find this out from pycurl.c
                        pycurl.INFOTYPE_DATA_OUT,           #4
                        pycurl.INFOTYPE_SSL_DATA_OUT]       #6
        curl_in  =  [   pycurl.INFOTYPE_HEADER_IN,          #1
                        pycurl.INFOTYPE_DATA_IN,            #3
                        pycurl.INFOTYPE_SSL_DATA_IN]        #5
        curl_info = [   pycurl.INFOTYPE_TEXT]               #0 

        if debug_type in curl_out:
            logger.debug("> %s" % debug_msg.strip())
        elif debug_type in curl_in:
            logger.debug("< %s" % debug_msg.strip())
        else:
            logger.debug("I %s" % (debug_msg.strip()) )
Example #13
0
    def _curl_log(self, debug_type, debug_msg):
        curl_out = [
            pycurl.INFOTYPE_HEADER_OUT,  #2  find this out from pycurl.c
            pycurl.INFOTYPE_DATA_OUT,  #4
            pycurl.INFOTYPE_SSL_DATA_OUT
        ]  #6
        curl_in = [
            pycurl.INFOTYPE_HEADER_IN,  #1
            pycurl.INFOTYPE_DATA_IN,  #3
            pycurl.INFOTYPE_SSL_DATA_IN
        ]  #5
        curl_info = [pycurl.INFOTYPE_TEXT]  #0

        if debug_type in curl_out:
            logger.debug("> %s" % debug_msg.strip())
        elif debug_type in curl_in:
            logger.debug("< %s" % debug_msg.strip())
        else:
            logger.debug("I %s" % (debug_msg.strip()))
Example #14
0
    def send_request(self, verb, url, data, headers={}):
        logger.info('ll httplibcurl -X "%s" "%s" ', verb, url)
        for (k, v) in headers.items():
            logger.debug('> %s: %s' % (k, v))
        logger.debug('\n')
        logger.debug('> ' + shorten(data, 1024))
        o = urlparse(url)
        host = o.netloc
        path = o.path
        if o.query:
            path += '?'
            path += o.query

        if o.scheme == 'https':
            conn = httplib.HTTPSConnection(host)
        else:
            conn = httplib.HTTPConnection(host)
        conn.request(verb, path, data, headers)
        response = conn.getresponse()
        return response
Example #15
0
def assert_headers(r1, r2, header_list = common_header_list):
    logging.debug('assert_headers %s', str(header_list) )
    for h in header_list:
        assert(r1['header'][h]  == r2['header'][h] )
Example #16
0
def assert_headers(r1, r2, header_list=common_header_list):
    logging.debug('assert_headers %s', str(header_list))
    for h in header_list:
        assert (r1['header'][h] == r2['header'][h])
Example #17
0
elif Setting.runtime == "BAE":
    try:
        from bae.api.memcache import BaeMemcache
        mc = BaeMemcache()
    except:
        error("BAE Memcache Error")
    
    from bae.core import const
    from bae.api import bcs
    from bae.api import logging
    
    BaeHost = const.BCS_ADDR
    #logging.debug(BaeHost + Setting.AK + Setting.SK)
    baebcs = bcs.BaeBCS(BaeHost, const.ACCESS_KEY, const.SECRET_KEY)
    logging.debug(baebcs.get_acl(Setting.bucket_domain, ' '))
    def save(filename, filedata):
        filename = filename.replace('/', '-')
        errcode, response = baebcs.put_object(Setting.bucket_domain, filename, filedata)
        if errcode != 0:
            raise Exception(response)
        return "http://" + BaeHost + "/" + Setting.bucket_domain + "/" + filename
else:
    try:
        if Setting.memcache:
            from libs import memcache
            mc = memcache.Client([Setting.memcache], debug=0)
    except:
        error("Memcache Error")
    def save(filename, filedata):
        f = None